这不是我不想更新视图,我只是不明白为什么它会在我不期望它时更新它(因为对象是在NG外部更新的)。Angular 2更新查看它什么时候不应该?
好的。
我有这个简单的代码:
@Component({
selector: 'my-app',
template: `
<div>
<input type='button' value="Fill more" (click)="fillMore()"/>
<ul>
<li *ngFor="let item of Items ">
{{item}}
</li>
</ul>
</div>
`,
})
export class App {
Items:Array<number> = new Array<number>();
constructor(private mySignalRService:MySignalRService)
{
this.Items.push(...[1,2,3]); //initial values
}
fillMore()
{
this.mySignalRService.go(this.Items);
}
}
正如你看到的,我也注入服务:
@Injectable()
export class MySignalRService
{
makeDummyDb:Promise<Array<number>>()
{
return new Promise((v,x)=> {
setTimeout(function(){v([100,200,300]);},800);
})
}
go(arr : Array<number>) : number
{
this.makeDummyDb().then((newValues)=>arr.push(...newValues)) ;
return 5; //this method must return a number
}
}
现在 - 当我点击按钮(在“填写更多“按钮),我做看到附加的项目,但是当我想起它,它不是因为更新因为我相信makeDummyDb:Promise
是在角的区域外(我可能在这里是错误的)
问:
是不是阵列更新发生外角的区域?我如何模拟一个真正的外部区域,以便代码无法工作? (用于测试)
看看这个运行的东西zone.js之外 http://stackoverflow.com/questions/43121400/run-ngrx-effect-outside-of -angulars-zone-to-prevent-timeout-in-massractor – intekhab