I have data to be rendered in the table. Let's consider these 5 rows -
[
{
"id": 1,
"name": "Name 1",
"type": "1"
},
{
"id": 2,
"name": "Name 2",
"type": "2"
},
{
"id": 3,
"name": "Name 3",
"type": "2"
},
{
"id": 4,
"name": "Name 4",
"type": "1"
},
{
"id": 5,
"name": "Name 5",
"type": "2"
}
]
Here is the template to render the data -
<table>
<thead>
<tr>
<th>id</th>
<th>name</th>
<th>type</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let d of data;">
<td width="30">{{ d.id }}</td>
<td width="80">{{ d.name }}</td>
<td>{{ getType(d.type) }}</td>
</tr>
</tbody>
</table>
As you can see, the last column will print some string based on the type, here goes the method -
/**
* Return string based on the type
*/
getType(type: string) {
console.log("modify data");
if (type === "1") {
return "First";
} else if (type === "2") {
return "Second";
}
}
Once, the component loads, this method prints "modify data" multiple times. Here is the snippet from the console.
Not sure if this is a common or an angular bug. Why is this method getting triggered 20 times?