I'm using a function that returns a boolean to set the items visibility using the hidden attribute inside a ngFor loop.
const countries = [
{country: 'USA', hide: 'false'},
{country: 'UK', hide: 'false'},
{country: 'Germany', hide: 'true'},
{country: 'France', hide: 'true'},
{country: 'Japan', hide: 'false'},
{country: 'Russia', hide: 'false'}
]
@Component({
selector: 'my-app',
template: `
<div>
<h2>Hello {{name}}</h2>
</div>
<my-list></my-list>
`,
})
export class App {
name:string;
constructor() {
this.name = `Angular! v${VERSION.full}`
}
}
@Component({
selector: 'my-list',
template: `
<ul>
<li *ngFor="let l of list" [hidden]="setVisibility(l)">{{l.country}}</li>
</ul>
`,
})
export class List implements OnInit {
list;
ngOnInit(){
this.list = countries
}
setVisibility(country){
console.log('setting');
let hide = false;
if(country.hide === 'true'){
hide = true;
}
return hide;
}
}
I put a console.log inside the setVisibility method to check how many times this method is called. I expected it to be called 6 times (1 time per item) but it's actually called 24 times ( 4 times per item). Why is this method called so many times? plunker
[hidden]="l.hide === 'true'"- Reza