我不太确定如何以更好的方式表达问题,但是我需要一些帮助以了解如何解决该问题。以下是我的错误:
TypeError: _co.create is not a function
TypeError: Cannot read property 'name' of undefined
当我尝试newCategory.name
在html中使用时,它将在控制台中引发这些错误。因此,我认为问题出在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>
您的HTML没问题。
问题在于newCategory: Category;
值未初始化。您在这里所做的只是将newCategory设置为type of
Category。
您可以通过以下方式初始化newCategory:
new
运算符 newCategory: Category = new Category();
export const initialState: Category = {
name: '',
description: ''
};
newCategory: Category = {
name: '',
description: ''
};
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句