最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Show empty value if undefined - Stack Overflow

programmeradmin2浏览0评论

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
Add a ment  | 

4 Answers 4

Reset to default 9

Use ? 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>
                &nbsp;
              </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

发布评论

评论列表(0)

  1. 暂无评论