I am trying to evaluate the below in template syntax which is an array:
FAIL {{ cols.length }}
I get the below error.
platform-browser.umd.js:1900 ORIGINAL EXCEPTION: TypeError: Cannot read property 'length' of undefined
But I do iterate so I know that portion works:
SUCCESS <th *ngFor="let h of cols"> {{h}} </th>
Using sizeof() function to Find Array Length in C++ Hence, if we simply divide the size of the array by the size acquired by each element of the same, we can get the total number of elements present in the array.
Basically, the length of an array is the total number of the elements which is contained by all the dimensions of that array. Property Value: This property returns the total number of elements in all the dimensions of the Array. It can also return zero if there are no elements in the array.
To find the length of an array, reference the object array_name. length. The length property returns an integer.
To find the length of an array in Python, we can use the len() function. It is a built-in Python method that takes an array as an argument and returns the number of elements in the array. The len() function returns the size of an array.
Use
{{ cols?.length }}
Or
<div *ngIf="cols">{{ cols.length }}</div>
If you want to print 0 for empty array, use
{{ cols?.length || '0' }}
Reason: cols
is not initiated when Angular2 load the template. And we want to wait until it's ready to access its members.
Try defining the cols variable as an object list on .ts
archive cols: object[]
;
If you want to print 0 for an empty array use the ternary operator:
{{ cols ? cols.length : '0' }}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With