Display output and also showing type error in angular?

152 Views Asked by At

Actually it is printing the correct output in the UI. But in the console it is displaying a type error?... How can I resolve this?

<tr>
 <th scope="row">IP addrress</th>
  <td>{{deviceinfo['ip-address']}}</td>
</tr>

ERROR TypeError: Cannot read property 'ip-address' of undefined

2

There are 2 best solutions below

0
Muhammed Albarmavi On

this may happen in case deviceinfo was not assign yet as example you get the value by http request

you can use ngIf to display the deviceinfo information when it has a value or truthy

<ng-container *ngIf="deviceinfo">
   <td>{{deviceinfo['ip-address']}}</td>
<ng-container>

another option is to set deviceinfo property to empty object

component

 public deviceinfo:any = {};
0
Dominik Schuster On

Or you could do:

<tr>
<th scope="row">IP addrress</th>
<td>{{deviceinfo?.ip-address}}</td>
</tr>

for the short form