5

I tried code follow:

@Component({
  selector: 'test-content',
  template: '
    <div *ngIf="sv.name.notExist.testUndefined != undefined">
    {{sv.name.notExist.testUndefined}}
    ',
  directives: [FORM_DIRECTIVES]
})

The variable sv.name.notExist.testUndefined is undefined, but i check it with *ngIf and the result is error with message: "TypeError: Cannot read property 'testUndefined' of undefined in [sv.name.notExist.testUndefined != undefined in ..."

Please help me check variable undefined with *ngIf.

4

1 回答 1

14

I think that you should use the elvis operator

<div *ngIf="sv?.name?.notExist?.testUndefined">

This link could give you more details: https://angular.io/docs/ts/latest/guide/template-syntax.html. See the section "The Elvis Operator ( ?. ) and null property paths".

Hope it helps you, Thierry

于 2016-01-08T09:41:33.157 回答