Kri*_*s-I 1 asp.net-web-api typescript angular angular5 angular6
屏幕上显示的值是正确的,但我在日志中收到此错误:ERROR TypeError: Cannot read property 'name' of undefined。
我用 '?' 尝试了几种方法 像这样:
[(ngModel)]="customer?.name"但不起作用,那么我需要帮助:)
在服务中:
getCustomer(name:string){
let url = `http://localhost:8081/api/customer/${name}`;
return this.http.get<Customer>(url);
}
Run Code Online (Sandbox Code Playgroud)
在组件的 .ts 中:
customer: Customer;
this.customerService.getCustomer(this.pName)
.subscribe(
(data) =>{
this.customer = data['customer'];
},
err =>{
console.log('getCustomer failed');
}
);
Run Code Online (Sandbox Code Playgroud)
在模板中:
<input type="text" class="form-control" id="myname" required [(ngModel)]="customer.name" name="myname">
Run Code Online (Sandbox Code Playgroud)
但这不行。
你有想法吗 ?
谢谢,
添加默认值
public customer = {} as Customer;
Run Code Online (Sandbox Code Playgroud)
和这个
<input type="text" class="form-control" id="myname" required [(ngModel)]="customer?.name" name="myname">
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10124 次 |
| 最近记录: |