How to check Unable to read testUndefined property from undefined in

I tried the following code:

@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 an error message: "TypeError: it is not possible to read the testUndefined property from undefined in [sv.name.notExist.testUndefined! = Undefined in ... "

Please help me check the undefined variable on * ngIf.

+5
source share
1 answer

I think you should use the elvis operator

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

This link can provide you with more detailed information: https://angular.io/docs/ts/latest/guide/template-syntax.html . See Section "Elvis Statement (?.) And Empty Property Paths".

Hope this helps you, Thierry

+14
source

Source: https://habr.com/ru/post/1240154/


All Articles