RK6
RK6

Reputation: 423

Using Angular2 - safe navigation operatore. Is there any option to check indexed array in html. For example: *ngIf="data && data.text[0]"

Using Angular2 - safe navigation operator. Is there any option to check indexed array in html. This condition is required, when it may be a string.

For example:

*ngIf="data && data.text[0]"

Here in data.text[0]. Cant we check like data.text?[0] or any other alternative is there. I am getting template parsing error if i used like this data.text?[0]

Upvotes: 0

Views: 128

Answers (1)

user4676340
user4676340

Reputation:

You don't have to. here are examples :

text[0] = ''; // false
text[0] = null; // false
text[0] = undefined; // false
text[0] = 0; // false
text[0] = 'any text with at least one letter'; // true
text[0] = 1; // any number more than 0 // true

You don't need the operator (which I forgot the name), because you're testing a value directly, not a value inside an object. So, it depends on the value in your array, which is related to the truthy/falsy values of javascript.

Upvotes: 0

Related Questions