Angular 角度4.X.X键值不同-如何使用?
正如标题所述,我试图理解如何在Angulars的后续版本中正确使用KeyValueDifferent。问题不在于它本身的功能,而是当我创建它时,它说create()由于对ChangeDetectorRef的去润滑而被去润滑Angular 角度4.X.X键值不同-如何使用?,angular,deprecated,Angular,Deprecated,正如标题所述,我试图理解如何在Angulars的后续版本中正确使用KeyValueDifferent。问题不在于它本身的功能,而是当我创建它时,它说create()由于对ChangeDetectorRef的去润滑而被去润滑 this.diff = this.keyValueDiffer.find(obj).create(null); 现在该如何使用 在Angular 4.1.3和4.3.3上试过 导入{ 组成部分, 奥尼特, 多切克, 关键值不同, 关键值不同, }从“@angular/cor
this.diff = this.keyValueDiffer.find(obj).create(null);
现在该如何使用
在Angular 4.1.3和4.3.3上试过
导入{
组成部分,
奥尼特,
多切克,
关键值不同,
关键值不同,
}从“@angular/core”开始;
@组成部分({
选择器:“我的组件”,
templateUrl:“./my.component.html”,
样式URL:['./my.component.scss']
})
导出类MyComponent实现OnInit、DoCheck
{
private _objDiff:{[any:string]:keyValueDifference};
私有_dataToWatch:{[any:string]:any};
公共构造函数(
private _keyvaluedifferents:keyvaluedifferents)
{
这个._objDiff={};
}
public ngOnInit():void
{
这.\u dataToWatch=//从以下位置获取数据。。。
this._keyvaluedifferences.find(this._dataToWatch.create();
}
public ngDoCheck():void
{
const changes=this.\u objDiff.diff(this.\u dataToWatch);
如果(更改)
{
更改。forEachChangedItem((更改)=>
{
if(change.currentValue!=change.previousValue)
{
log(“值已更改!”);
}
});
}
}
}
有关更多信息,请参阅:1.
2.
3.
请注意,使用
IterableDiffer
和IterableDiffer
服务(而不是keyvaluedifferences
和keyvaluedifferences
)以不同的方式执行数组更改检测。您到底想用它做什么?因此,用法应该是我有一个对象,我希望对其执行更改检测(对象不更改,仅更改其中的值)。我想为对象设置一个原始值,然后在ngDoCheck()中执行手动更改检测。这方面有一个很好的例子,请在问题中加入一些有用的上下文,外部资源仅供进一步阅读。
import {
Component,
OnInit,
DoCheck,
KeyValueDiffers,
KeyValueDiffer,
} from '@angular/core';
@Component({
selector: 'my-component',
templateUrl: './my.component.html',
styleUrls: ['./my.component.scss']
})
export class MyComponent implements OnInit, DoCheck
{
private _objDiff: { [any: string]: KeyValueDiffer<string, any> };
private _dataToWatch: { [any: string]: any };
public constructor(
private _keyValueDiffers: KeyValueDiffers)
{
this._objDiff = {};
}
public ngOnInit(): void
{
this._dataToWatch = // get the data from some where...
this._keyValueDiffers.find(this._dataToWatch).create();
}
public ngDoCheck(): void
{
const changes = this._objDiff.diff(this._dataToWatch);
if(changes)
{
changes.forEachChangedItem((change) =>
{
if (change.currentValue != change.previousValue)
{
console.log("Value was changed!");
}
});
}
}
}