【发布时间】:2016-06-29 16:39:35
【问题描述】:
我遇到了ngOnChange 的问题。我有以下组件:
@Component({
selector:'user-table',
template: `...`,
})
export class UserTable implements OnChanges{
@Input() users: User[];
years:string[];
constructor(private _usersCollection:UsersCollection){
}
ngOnChanges(){
if (this.users.length)
{this.years =this._usersCollection.createYearsArray(this.users)}
}
}
但是,如果条件只检查一次 - 当 this.users 尚未从服务器获取时,因此其长度为 0。我如何找到处理这种异步输入的解决方案?
数组已更新,就像我设置以下日志时一样:
console.log('ON FIRST INIT' , this.programs);
this.years = this._usersCollection.createYearsArray();
console.log(this.years);
setInterval(()=>{
console.log('IN INTERVVAL' , this.programs);
},1000);
控制台输出为:
ON FIRST INIT []
UsersTable.component.ts:21 []
UsersTable.component.ts:23 IN INTERVVAL [Object, Object, Object, Object]
【问题讨论】:
-
很难从这段代码中看出发生了什么。你在哪里获取数据。它们是如何传递给
UserTable组件的? -
我在车里 - 我会在 30' 后更新 q :)
标签: angular