And*_*esi 13 es6-promise nativescript angular2-nativescript 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
});
}
}
Run Code Online (Sandbox Code Playgroud)
相对的html是:
<Label *ngFor="let story of stories" [text]='story.message'></Label>
Run Code Online (Sandbox Code Playgroud)
当应用程序启动时,我只看到一次推送,但是我创建了一个触发"console.log(JSON.stringify(this.stories))"的按钮;" 在那一刻,当我点击按钮时,ui似乎检测到更改的数组,并出现另一个推送的对象.
编辑:
我在这个帖子中创建了一个更简单的例子:Angular 2:当我在ngOnInit中更改promise.than中的变量时,视图不会刷新
nic*_*oon 20
更改检测基于引用,将元素推送到数组不会触发它.尝试像这样更新引用:
this.stories.push(story);
this.stories = this.stories.slice();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8216 次 |
| 最近记录: |