【发布时间】:2019-03-02 12:07:49
【问题描述】:
我创建了一个 Angular 2 的 NativeScript 应用程序,我有一个我希望在应用程序前端看到的对象数组。行为是,如果我将一个对象直接推入 ngOnInit() 内部的数组中,它会起作用,但如果我在 ngOnInit() 中创建一个承诺,它就不起作用。这是代码:
export class DashboardComponent {
stories: Story[] = [];
pushArray() {
let story:Story = new Story(1,1,"ASD", "pushed");
this.stories.push(story);
}
ngOnInit() {
this.pushArray(); //this is shown
var promise = new Promise((resolve)=>{
resolve(42);
console.log("promise hit");
});
promise.then(x=> {
this.pushArray(); //this is NOT shown
});
}
}
相对html为:
<Label *ngFor="let story of stories" [text]='story.message'></Label>
当应用程序启动时,我只看到一个推送,但我创建了一个按钮来触发“console.log(JSON.stringify(this.stories));”在那一刻,当我点击按钮时,用户界面似乎检测到更改的数组,并出现另一个推送的对象。
编辑:
我在这个帖子中创建了一个更简单的例子:Angular 2: when i change a variable in a promise.than in ngOnInit the view doesn't refresh
【问题讨论】:
-
如果您重新加载页面,是否会出现更改?
标签: angular nativescript es6-promise angular2-nativescript