In an Angular 5 project, I have the following in:
inventory.component.ts:
inventoryItems: any;
constructor(private router: Router,) {
}
ngOnInit() {
this.inventoryItems=[];
}
I reference the variable like this in inventory.component.html:
<span class="indicator">{{inventoryItems.length}}</span>
The browser console is complaining about:
ERROR TypeError: Cannot read property 'length' of null
If the property is getting initialized in ngOnInit, why would it be null? It should be zero.
this.inventoryItemsis anobservable, then you could try doing{{ inventoryItems.length | async }}within the HTML. This will tell the template to wait until theobservableis resolved.this.inventoryItems=[];ngOnInitis not running before the template first attempts to accessinventoryItems.inventoryItemsis null. Because it won't be null, and the code above won't throw an error. If it's an input, this should be reflected in the question. Please, provide stackoverflow.com/help/mcve that can be replicated.