2017-11-24 7 views
3

이 변수를 입력하여 선언해야합니까? 이것은 비주얼 코드 편집기에서 HTML 템플릿으로 나타납니다.'필수'식별자가 정의되지 않았습니다. '__type'에 해당 멤버가 없습니다.

enter image description here

제품 form.component.html

<div class="row"> 

    <div class="col-md-6"> 
     <form #form="ngForm" (ngSubmit)="save(form.value)"> 

      <div class="form-group"> 
       <label for="title">Title</label> 
       <input #title="ngModel" [(ngModel)]="product.title" name="title" id="title" type="text" class="form-control" required> 
       <div class="alert alert-danger" *ngIf="title.touched && title.invalid"> 
       Title is required. 
       </div> 
      </div> 

      <div class="form-group"> 
       <label for="price">Price</label> 
       <div class="input-group"> 
        <span class="input-group-addon">$</span> 
        <input #price="ngModel" ngModel [(ngModel)]="product.price" name="price" id="price" type="number" class="form-control" required [min]="0"> 
       </div> 
       <div class="alert alert-danger" *ngIf="price.touched && price.invalid"> 
        <div *ngIf="price.errors.required">Price is required.</div> 
        <div *ngIf="price.errors.min">Price should be 0 or higher.</div> 
       </div> 
      </div> 

      <div class="form-group"> 
       <label for="category">Category</label> 
       <select #category="ngModel" ngModel [(ngModel)]="product.category" name="category" id="category" class="form-control" required> 
        <option value=""></option> 
        <option *ngFor="let category of categories$ | async" [value]="category.key">{{ category.payload.val().name }}</option> 
       </select> 
       <div class="alert alert-danger" *ngIf="category.touched && category.invalid"> 
        Category is required. 
       </div> 
      </div> 

      <div class="form-group"> 
       <label for="imageUrl">Image URL</label> 
       <input #imageUrl="ngModel" ngModel [(ngModel)]="product.imageUrl" name="imageUrl" id="imageUrl" type="text" class="form-control" required url> 
       <div class="alert alert-danger" *ngIf="imageUrl.touched && imageUrl.invalid"> 
        <div *ngIf="imageUrl.errors.required">Image URL is required.</div> 
        <div *ngIf="imageUrl.errors.url">Please enter a valid URL.</div> 
       </div> 
      </div> 

      <button class="btn btn-primary">Save</button> 

      </form> 
    </div> 

    <div class="col-md-6"> 
     <div class="card" style="width: 20rem;"> 
      <img class="card-img-top" [src]="product.imageUrl" *ngIf="product.imageUrl"> 
      <div class="card-block"> 
      <h4 class="card-title">{{ product.title }}</h4> 
      <p class="card-text">{{ product.price | currency: 'USD': symbol }}</p> 
      </div> 
     </div> 
    </div> 

</div> 

제품 form.component.ts 나는이 오류를 제거하려면 어떻게

import { Component, OnInit } from '@angular/core'; 
import { CategoryService } from '../../category.service'; 
import { ProductService } from '../../product.service'; 
import { Router } from '@angular/router'; 
import { ActivatedRoute } from '@angular/router'; 
import 'rxjs/add/operator/take'; 

@Component({ 
    selector: 'app-product-form', 
    templateUrl: './product-form.component.html', 
    styleUrls: ['./product-form.component.css'] 
}) 
export class ProductFormComponent implements OnInit { 

    categories$; 
    product: any = {}; 

    constructor(
    private router: Router, 
    private route: ActivatedRoute, 
    private categoryService: CategoryService, 
    private productService: ProductService) { 
    this.categories$ = categoryService.getCategories(); 

    let id = this.route.snapshot.paramMap.get('id'); 
    if (id) { 
     this.productService.get(id).take(1).subscribe(p => this.product = p); 
    } 
    } 

    save(product) { 
    this.productService.create(product); 
    this.router.navigate(['/admin/products']); 
    } 

    ngOnInit() { 
    } 

} 

? 앱의 기능이 영향을 미치지 않는 것 같습니다. 그래서 그것은 내가 이해하는 것으로부터 유효한 JS로 컴파일됩니다. 객체를 "any"로 선언하여 객체를 수정할 수있었습니다.

가능한 경우이 인터페이스를 설정하는 방법을 배우는 데 흥미가 있습니다.

+1

은 아무데도 당신이 any''이외의 시도 우리가 참조 할 수 없습니다. 이 오류를 보여주는 stackblitz를 만들 수 있다면 또한 좋을 것입니다 :) – Alex

답변

2

"?" "가격"이후. 이처럼

:

<div *ngIf="price?.errors.required">Price is required.</div> 
<div *ngIf="price?.errors.min">Price should be 0 or higher.</div> 
+0

이것은 나를 위해 일했습니다. – Rhokai