angular - 错误类型错误:无法在 Object.eval [as updateRenderer] 处读取 null 的属性“电话”
问题描述
我有这个从 api 获取所有客户端的函数:
this.ws.getallclients().subscribe(
client => {
this.client = client.map((clients) => {
this.filteredOptions = this.addsale.controls['client_id'].valueChanges.pipe(
startWith(''),
map(val => this._filter(val))
);
return new Client(clients);
});
if (this.ss.getData('client_id')) {
const client_id = this.ss.getData('client_id');
this.addsale.controls.client_id.setValue(client_id)
let selectedClient = new Client('')
this.selectedClient = null;
for (let i = 0; i < client.length; i++) {
if (client[i].client_id === client_id) {
this.selectedClient = client[i];
}
}
}
}
);
在此显示错误中:
错误类型错误:无法在 Object.eval [as updateRenderer] 处读取 null 的属性“电话”
因为 client[i].client_id 返回 client_id 像 123456 === 和 client_id 显示客户端的名称,如 MyName 和这个 html 代码:
我的html代码:
<fieldset>
<legend>Client Data</legend>
<div class="input-field col s4">
<input formControlName="client_id" id="client_id" matInput placeholder="Select Client*" aria-label="State" [matAutocomplete]="auto" >
<mat-autocomplete autoActiveFirstOption #auto="matAutocomplete" [displayWith]="displayFn">
<mat-option (onSelectionChange)="onSelect(item.client_id)" *ngFor="let item of filteredOptions | async" [value]="item.name">
{{item.name}}
</mat-option>
</mat-autocomplete>
</div>
<div class="input-field col s12">
ID Number:
<span style="color:darkblue">{{selectedClient.phone}}</span>
</div>
</fieldset>
知道如何发出此错误吗?
解决方案
this.selectedClient = null
如果没有选定的客户端,您的代码设置,然后您的模板尝试访问selectedClient.phone
,导致错误。也许您打算替换这些行:
let selectedClient = new Client('')
this.selectedClient = null;
和:
this.selectedClient = new Client('')
这样您Client
在这种情况下就有一个空白对象。或者,ngIf
如果没有selectedClient
. 我不知道 Angular,但根据对文档的快速阅读,这可能有效:
<span *ngIf="selectedClient" style="color:darkblue">{{selectedClient.phone}}</span>
推荐阅读
- font-awesome - FontAwesome 图标在 css 内容中不起作用
- c++ - 将带有时区的纪元时间转换为 Excel 格式时间?
- node.js - 从 AWS-lambda 函数控制台发布一个 http 请求,使用 Alexa 作为输入
- android - 在第二个 notifyDataSetChanged 上刷新适配器?
- angular - 获取激活路由的路由数据和查询参数
- vba - 需要将单元格中的数据拆分为不同的列,并帮助使用 vba 将数据从网站复制到 excel
- excel - 确定列中一个值的范围,用于在另一个相同范围内运行函数
- r - 如何计算 LASSO 回归的交叉验证 R2?
- neo4j - 将边缘加载到 neo4j 需要太多时间
- multi-tenant - 来自不同租户的多个虚拟机