【发布时间】:2016-05-11 15:09:27
【问题描述】:
我使用 beta.0 是因为 this outstanding bug 阻止 Angular 2 在 IE 中的 beta.1 和 beta.2 中工作。
SearchBar.ts 中的相关代码
@Component({
selector : 'search-bar',
templateUrl: 'views/searchbar.html'
})
export class SearchBar {
private history: SearchHistoryEntry[] = [];
@Output() onHistory = new EventEmitter();
constructor() {
this.history = JSON.parse(localStorage.getItem('SearchHistory')) || [];
}
ngOnInit() {
// The constructor doesn't have @Outputs initialized yet. Emit from this
// life cycle hook instead to be sure they're received on the other side
debugger;
this.onHistory.emit(this.history);
}
}
来自home.html的相关代码
<search-bar (onHistory)="SearchBarHistory($event)"></search-bar>
home.ts 中的相关代码
SearchBarHistory(history: SearchHistoryEntry[]) {
debugger;
this.history = history;
}
在 Chrome 中这工作得很好。 SearchBar 的构造函数正确地从localStorage 读取,在ngOnInit 中它发出给接收它的我的 Home 组件,它存储在本地,并且绑定到history 的 UI 绑定更新以显示所有应有的信息。
在 IE 11 中这不起作用。 ngOnInit 在我点击搜索栏后才会运行。似乎任何@Input 或生命周期挂钩(特别是我已经测试过ngOnInit、ngAfterContentInit 和ngAfterViewInit 并且它们的行为都相同)在触发组件的更改检测之前不会运行。如果我刷新页面,那么它的运行方式与 Chrome 完全一样,@Inputs 或生命周期钩子无需交互即可调用,并且我的历史记录会按照应有的方式进行绑定。
我认为这是测试版的一个错误,但与此同时,我可以做些什么来让它在没有交互或页面刷新的情况下第一次工作?
【问题讨论】:
-
用打字稿写这个例子与问题无关。最后编译成JS,问题依旧存在。我要删除 typescript 标签。
标签: internet-explorer-11 angular