如何侦听 Angular 2 中代码的变化
how can I listen to changes in code in angular 2?
我正在使用角度2。我有一个带有输入的组件。我希望能够在输入值更改时编写一些代码。绑定正在工作,如果数据被更改(从组件外部),我可以看到 dom 发生了变化。
@Component({
selector: 'test'
})
@View({
template: `
<div>data.somevalue={{data.somevalue}}</div>`
})
export class MyComponent {
_data: Data;
@Input()
set data(value: Data) {
this.data = value;
}
get data() {
return this._data;
}
constructor() {
}
dataChagedListener(param) {
// listen to changes of _data object and do something...
}
}
您可以使用生命周期钩子ngOnChanges
:
export class MyComponent {
_data: Data;
@Input()
set data(value: Data) {
this.data = value;
}
get data() {
return this._data;
}
constructor() {
}
ngOnChanges([propName: string]: SimpleChange) {
// listen to changes of _data object and do something...
}
}
在以下情况下触发此挂钩:
如果任何绑定已更改
有关更多详细信息,请参阅以下链接:
- https://angular.io/docs/ts/latest/guide/lifecycle-hooks.html
- https://angular.io/docs/ts/latest/api/core/OnChanges-interface.html
正如 Thierry Templier 的答案评论中提到的,ngOnChanges
生命周期钩子只能检测对原语的更改。我发现通过使用ngDoCheck
,您可以手动检查对象的状态以确定对象的成员是否已更改:
完整的Plunker可以在这里找到。但这是重要的部分:
import { Component, Input } from '@angular/core';
@Component({
selector: 'listener',
template: `
<div style="background-color:#f2f2f2">
<h3>Listener</h3>
<p>{{primitive}}</p>
<p>{{objectOne.foo}}</p>
<p>{{objectTwo.foo.bar}}</p>
<ul>
<li *ngFor="let item of log">{{item}}</li>
</ul>
</div>
`
})
export class ListenerComponent {
@Input() protected primitive;
@Input() protected objectOne;
@Input() protected objectTwo;
protected currentPrimitive;
protected currentObjectOne;
protected currentObjectTwo;
protected log = ['Started'];
ngOnInit() {
this.getCurrentObjectState();
}
getCurrentObjectState() {
this.currentPrimitive = this.primitive;
this.currentObjectOne = _.clone(this.objectOne);
this.currentObjectTwoJSON = JSON.stringify(this.objectTwo);
}
ngOnChanges() {
this.log.push('OnChages Fired.')
}
ngDoCheck() {
this.log.push('DoCheck Fired.');
if (!_.isEqual(this.currentPrimitive, this.primitive)){
this.log.push('A change in Primitive''s state has occurred:');
this.log.push('Primitive''s new value:' + this.primitive);
}
if(!_.isEqual(this.currentObjectOne, this.objectOne)){
this.log.push('A change in objectOne''s state has occurred:');
this.log.push('objectOne.foo''s new value:' + this.objectOne.foo);
}
if(this.currentObjectTwoJSON != JSON.stringify(this.objectTwo)){
this.log.push('A change in objectTwo''s state has occurred:');
this.log.push('objectTwo.foo.bar''s new value:' + this.objectTwo.foo.bar);
}
if(!_.isEqual(this.currentPrimitive, this.primitive) || !_.isEqual(this.currentObjectOne, this.objectOne) || this.currentObjectTwoJSON != JSON.stringify(this.objectTwo)) {
this.getCurrentObjectState();
}
}
应该注意的是,Angular 文档提供了有关使用 ngDoCheck
的警告:
虽然 ngDoCheck 钩子可以检测英雄的名字何时更改, 它有一个可怕的成本。这个钩子的调用频率很高—— 在每个更改检测周期之后,无论更改在哪里 发生。在此示例中,它在 用户可以做任何事情。
这些初始检查中的大多数是由Angular的第一个触发的。 在页面上的其他位置呈现不相关的数据。仅仅将鼠标拖入 另一个输入框触发呼叫。相对较少的呼叫显示实际 对相关数据的更改。显然,我们的实施必须非常 轻量级或用户体验将受到影响。
相关文章:
- 从桌面读取python文件时高亮显示代码
- 如何将函数包装在函数中以避免代码重复
- 通过单击表单中的按钮,在代码生成中使用javascript生成字母数字代码
- 货币代码为欧元-金额的格式不应包含小数
- Regex代码只允许一个空格
- 如何将屏幕分辨率乘以 80%,然后在代码中使用
- 从var向代码隐藏函数传递值
- 如何动态插入jquery代码
- Javascript:Coderbyte挑战4:字母变化:帮助分析代码:这是如何大写元音的,以及何时执行else语句
- 如果我在网站上运行IE8调试器,代码会发生什么变化
- 如何侦听 Angular 2 中代码的变化
- 有没有办法在 javascript 中的值发生变化时运行代码
- 为什么这个JS代码段中的上下文会发生变化
- 即使图像路径发生变化,Javascript代码也会绘制相同的图像
- 检测哈希变化和哈希存在的代码运行副本
- 当我插入MySQL时,我的代码发生了变化
- Lodash从3到4的变化是什么,这段代码不能工作
- 当.scss文件中的代码发生变化时,在gulpfile中运行elixir code
- 当季节背景发生变化时,我如何修改代码,使html和jscript同时工作
- 为什么这段代码不能识别绑定到ObservableArray上的变化?