मैं कोणीय का उपयोग करने की कोशिश के साथ एक समस्या हो रही है *ngForऔर *ngIfएक ही तत्व पर।
जब संग्रह के माध्यम से लूप की कोशिश की जाती है *ngFor, तो संग्रह को nullइसके गुणों के रूप में देखा जाता है और परिणामस्वरूप विफल हो जाता है।
@Component({
selector: 'shell',
template: `
<h3>Shell</h3><button (click)="toggle()">Toggle!</button>
<div *ngIf="show" *ngFor="let thing of stuff">
{{log(thing)}}
<span>{{thing.name}}</span>
</div>
`
})
export class ShellComponent implements OnInit {
public stuff:any[] = [];
public show:boolean = false;
constructor() {}
ngOnInit() {
this.stuff = [
{ name: 'abc', id: 1 },
{ name: 'huo', id: 2 },
{ name: 'bar', id: 3 },
{ name: 'foo', id: 4 },
{ name: 'thing', id: 5 },
{ name: 'other', id: 6 },
]
}
toggle() {
this.show = !this.show;
}
log(thing) {
console.log(thing);
}
}
मुझे पता है कि आसान समाधान *ngIfएक स्तर को ऊपर ले जाने के लिए है, लेकिन एक सूची में आइटमों पर लूपिंग जैसे परिदृश्यों के लिए ul, मैं liसंग्रह खाली होने पर या तो खाली के साथ समाप्त होता हूं , या मेरा liनिरर्थक कंटेनर तत्वों में लिपटा हुआ है।
इस plnkr पर उदाहरण ।
कंसोल त्रुटि पर ध्यान दें:
EXCEPTION: TypeError: Cannot read property 'name' of null in [{{thing.name}} in ShellComponent@5:12]
क्या मैं कुछ गलत कर रहा हूँ या यह एक बग है?