【发布时间】:2019-10-03 16:53:31
【问题描述】:
我正在关注 Mosh Hamedani 教程“Angular 4:初学者到专业人士”。
我在尝试编辑产品时尝试在表单中显示产品的标题。 该产品存储在 Firebase 数据库中。 我是 Angular 的新手。
但是,当我转到编辑表单时,控制台中出现此错误
ERROR TypeError: Cannot read property 'title' of undefined
at Object.eval [as updateDirectives] (ProductFormComponent.html:7)
at Object.debugUpdateDirectives [as updateDirectives] (core.js:23911)
at checkAndUpdateView (core.js:23307)
at callViewAction (core.js:23548)
at execComponentViewsAction (core.js:23490)
at checkAndUpdateView (core.js:23313)
at callViewAction (core.js:23548)
at execEmbeddedViewsAction (core.js:23511)
at checkAndUpdateView (core.js:23308)
at callViewAction (core.js:23548)
这是我的表格的一部分:
<div class="form-group">
<label for="title">Title</label>
<input #title="ngModel" [(ngModel)]="product.title" <== error here
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>
这里是 product-form.component.ts
import { Component, OnInit, OnDestroy } from '@angular/core';
import { CategoryService } from 'src/app/category.service';
import { ProductService } from 'src/app/product.service';
import { Router, ActivatedRoute } from '@angular/router';
import { take } from 'rxjs/operators';
@Component({
selector: 'app-product-form',
templateUrl: './product-form.component.html',
styleUrls: ['./product-form.component.css']
})
export class ProductFormComponent implements OnInit {
categories$;
product;
constructor(
categoryService: CategoryService,
private route: ActivatedRoute,
private productService: ProductService,
private router: Router) {
this.categories$ = categoryService.getCategories();
let id = this.route.snapshot.paramMap.get('id');
if (id) this.productService.get(id).snapshotChanges().pipe(take(1))
.subscribe(p => this.product = p); <== this does not seem to be working
}
save(product) {
this.productService.create(product);
this.router.navigate(["/admin/products"]);
}
ngOnInit() {
}
}
如何显示产品的标题?
【问题讨论】:
-
试试 product['title'] 而不是 product.title
-
@PareshGami 它与符号无关,似乎OP没有及时得到适当的响应!
-
控制台“产品”并检查“标题”键是否可用
标签: angular typescript firebase