遇到错误后,更改检测似乎中断
Change detection appears to break after encountering error
更新看来Angular团队有望解决这个问题。
https://github.com/angular/angular/issues/2413
我遇到了一个问题,Angular(Beta 15)的更改检测在遇到错误后似乎停止了工作——我不确定Zone是否在错误后取消订阅更改。
再现错误的最简单方法是尝试引用null
或undefined
的属性。
这个问题可以在这里看到。
@Component({
selector: 'shell',
template: `
<h3>Shell</h3>
<p>{{dummy.myArray[0].text}}</p>
<button (click)="getUsers()">Get users!</button>
<button (click)="setDummyTextToNull()">Break stuff!</button>
<ul>
<li *ngFor="#user of users">{{user.login}}</li>
</ul>
`
})
export class ShellComponent implements OnInit {
public params;
public users;
public dummy;
constructor(private _router:Router,
public routeParams:RouteParams,
private _ghService:GithubService) {
this.params = this.routeParams;
this.subscribe();
}
ngOnInit() {
this.dummy = {
myArray: [
{ text: 'Some dummy text' }
]
};
}
setDummyTextToNull() {
this.dummy = null;
}
getUsers() {
this.getUsers();
}
subscribe() {
this._ghService.users$.subscribe(data => this.onResponse(data), err => {
console.warn('Could not fetch', err.status)
});
}
getUsers() {
this._ghService.loadUsers();
}
onResponse(data) {
console.log('Successfully fetched users', data);
this.users = this.shuffle(data);
}
shuffle(array) {
var currentIndex = array.length, temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
}
点击"获取用户"将获取github用户并打乱列表。重复执行此操作,您可以看到正在使用的列表。在强制模板引用null
的属性之后,更改检测似乎不再起作用。
如果我点击该按钮,我预计会重复出现该错误,但事实上,模板中的任何错误都会导致应用程序崩溃,需要刷新。
除了到处使用ngIf
和"elvis操作符"之外,有人经历过这种情况吗?
我认为这是按设计的。
如果您更改
{{dummy.myArray[0].text}}
至
{{dummy?.myArray[0].text}}
你不应该得到一个错误
相关文章:
- 如何检测是否有溢出
- 如何检测用于WebGL的专用或集成显卡
- 防止Alt+Shift默认操作或检测多种操作系统语言的Javascript
- 如何在使用selectBoxIt JQuery插件时检测选项更改
- 如何使用密码检测网络中的状态连接
- 使用JavaScript检测Flash
- 在不阻止默认行为的情况下检测IE10中的缩放
- 检测个位数整数时正在转换毫秒
- 检测电话窃听,即:<a href="电话:xxx">在UIWebview上
- 动态插入的表:JQuery未检测到最后一行
- 检测页面上某个元素中选择(突出显示)或单击的内容
- jQuery检测浏览器是否支持Zoom
- 高亮显示与数组字符串一起使用时文本插件中断
- 检测iframe是否跨域的愚蠢方法
- 检测图像分辨率
- 如何检测第三方广告服务器请求
- 遇到错误后,更改检测似乎中断
- 检测被用户操作中断的 Ajax 请求
- 检测 HTML5 音频流何时中断或暂停
- 是否有一种方法来检测css转换已经中止/中断