angular 2:錯(cuò)誤:TypeError:無(wú)法讀取屬性‘…’未定義我已經(jīng)附上了我的角度2代碼塊的柱塞。我想從我的JSON中打印一個(gè)字段,但是無(wú)法打印,因?yàn)樽畛跷业膶?duì)象是NULL,并且它是通過(guò)承諾填充的。這是我的組件文件import {Component, NgModule, OnInit} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'
class MyData {
xyz : MySubData;
}
class MySubData {
name : string;
}
@Component({
selector: 'my-app',
template: `
<div>
<h2>Hello {{name}}</h2>
{{abc.xyz.name}}
</div>
`,
})
export class App implements OnInit {
abc : MyData = null;
constructor() {
this.name = 'Angular2'
}
ngOnInit() {
setTimeout(() => {
this.abc = new MyData();
this.abc.xyz = new MySubData();
this.abc.xyz.name = "Binita";
}, 2000);
}
}
@NgModule({
imports: [ BrowserModule ],
declarations: [ App ],
bootstrap: [ App ]
})
export class AppModule {}當(dāng)我把線移開(kāi)的時(shí)候{{abc.xyz.name}}從我的模板來(lái)看,它運(yùn)行得很好。我在代碼中使用了SET TIME,因?yàn)槲艺趶闹Z言(即異步調(diào)用)中獲取數(shù)據(jù)。我最初能理解為abc是null,我的代碼找不到abc.xyz.name。但我不想讓任何條件檢查。因?yàn)槲以贘SON中有幾個(gè)屬性,所以每個(gè)屬性都不可能編寫(xiě)if條件。在angularjs 1的前面,如果ABC為NULL,那么它將自動(dòng)用空字符串替換它。我也想在英吉利里做同樣的事情。請(qǐng)建議。下面是柱塞https:/plnkr.co/EDIT/u1NqNF0penz7OS55QmoU?P=預(yù)覽
angular 2:錯(cuò)誤:TypeError:無(wú)法讀取屬性‘…’未定義
開(kāi)心每一天1111
2019-07-20 14:33:35