admin管理员组文章数量:1357291
I have angular ponent that get values from back end.
Here is the method in ts.
properties: PropertyDto[] = [];
getProperties(event?: LazyLoadEvent): void {
if (this.primengTableHelper.shouldResetPaging(event)) {
this.paginator.changePage(0);
return;
}
this.primengTableHelper.showLoadingIndicator();
this._propertyService.getProperties(
this.filterText,
this.primengTableHelper.getSorting(this.dataTable),
this.primengTableHelper.getMaxResultCount(this.paginator, event),
this.primengTableHelper.getSkipCount(this.paginator, event)
)
.pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator()))
.subscribe(result => {
this.primengTableHelper.totalRecordsCount = result.totalCount;
this.primengTableHelper.records = result.items;
this.primengTableHelper.hideLoadingIndicator();
});
}
Some of values in result can be null.
Here is html part for those values
<td>
{{record.landlord.name}}
</td>
<td>
{{record.agent.name}}
</td>
I have errors like
Cannot read property 'name' of undefined
How I can show just blank field and avoid those errors?
I have angular ponent that get values from back end.
Here is the method in ts.
properties: PropertyDto[] = [];
getProperties(event?: LazyLoadEvent): void {
if (this.primengTableHelper.shouldResetPaging(event)) {
this.paginator.changePage(0);
return;
}
this.primengTableHelper.showLoadingIndicator();
this._propertyService.getProperties(
this.filterText,
this.primengTableHelper.getSorting(this.dataTable),
this.primengTableHelper.getMaxResultCount(this.paginator, event),
this.primengTableHelper.getSkipCount(this.paginator, event)
)
.pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator()))
.subscribe(result => {
this.primengTableHelper.totalRecordsCount = result.totalCount;
this.primengTableHelper.records = result.items;
this.primengTableHelper.hideLoadingIndicator();
});
}
Some of values in result can be null.
Here is html part for those values
<td>
{{record.landlord.name}}
</td>
<td>
{{record.agent.name}}
</td>
I have errors like
Cannot read property 'name' of undefined
How I can show just blank field and avoid those errors?
Share Improve this question edited Apr 11, 2019 at 12:25 Liam 29.8k28 gold badges139 silver badges203 bronze badges asked Apr 11, 2019 at 12:21 Eugene SukhEugene Sukh 2,7275 gold badges51 silver badges97 bronze badges 2-
1
Try
{{record?.landlord?.name}}
(question marks) – porgo Commented Apr 11, 2019 at 12:24 - Possible duplicate of Error if don't check if {{object.field}} exists – Liam Commented Apr 11, 2019 at 12:43
4 Answers
Reset to default 9Use ?
for safe binding, try this:
{{record?.landlord?.name}}
Better. record?
could also be record.landlord?
depending on your datasource, i.e. can landlord be undefined if record isn't.
<td>
{{record? record.landlord.name :''}}
</td>
<ng-container *ngIf="record; else elseTemplate">
<td>
{{record.landlord.name}}
</td>
</ng-container>
<ng-template #elseTemplate>
<td>
</td>
</ng-template>
You could use ng ifs to show something different if there is no record for example:
<div *ngIf="record">
<h1 *ngIf="record.landlord">{{record.landlord.name}}</h1>
<h1 *ngIf="record.agent">{{record.agent.name}}</h1>
</div>
you can find more information/docs here
<td>
{{record.landlord? record.landlord.name :''}}
</td>
Checks the value of landlord
. If it is truthy
by the means of JavaScript, use it. Otherwise use empty string
本文标签: javascriptShow empty value if undefinedStack Overflow
版权声明:本文标题:javascript - Show empty value if undefined - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744069417a2585613.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论