问题
For some reason I can get this code to work in Chrome but did not find a solution for Safari and IE. I haven't tried Firefox yet. My project is using the typescript transpiler.
constructor(){
this.eventStream = Observable.fromEvent(window,'resize');
this.eventStream.subscribe((event) => this.updateWindow(event));
}
ngOnInit(){
this.updateWindow();
}
updateWindow(event:any){
this.updateHeight = window.innerHeight;
}
Plunker example
回答1:
Your code works in Firefox, but not in Safari and IE, it looks strange. My guess is that zone.js is responsible for that. Changed your example to invoke update through zone:
constructor(zone:NgZone)
{
this.eventStream = Observable.fromEvent(window,'resize');
this.eventStream.subscribe((event) => {zone.run(()=> {this.updateWindow(event)}) });
}
now it works in Safari plunkr
来源:https://stackoverflow.com/questions/36756196/how-do-i-get-an-observable-to-update-a-variable-in-ie