【发布时间】:2019-09-06 16:40:39
【问题描述】:
我不太确定如何更好地表达这个问题,但我 需要一些帮助来理解如何解决这个问题。以下是我的错误:
TypeError: _co.create is not a functionTypeError: Cannot read property 'name' of undefined
当我尝试在 html 中使用 newCategory.name 时,它会在控制台中引发这些错误。所以我认为问题出在 HTML 中。
newCategory 在 CreateCategoryComponent.ts
中定义newCategory: Category;
name: string;
description: string;
CategoryService.ts
//ommitted some redundant code
baseUrl: string = "api/Category";
createCategory(newCategory: Category) : Observable<any> {
//Not too sure if newCategory is added correctly here
return this.httpClient.get<any>(this.baseUrl+"username="+this.sessionService.getUsername()+"&password="+this.sessionService.getPassword() + newCategory).pipe (
catchError(this.handleError)
);
}
CreateCategory.html
<td><input id="name" name="name" #name="ngModel" type="text" [(ngModel)]="newCategory.name" required="true" /></td>
【问题讨论】:
-
newCategory: Category;这仅将 newCategory 定义为 Category 类型,但不会初始化值 -
newCategory在您的组件中声明,但未定义 -
@mxr7350 是的。我错过了。谢谢你。需要添加 this.newCategory = new Category();
标签: html angular typescript