简体   繁体   English

Angular2 KeyValueDiffers在init之后检测更改而不更改对象

[英]Angular2 KeyValueDiffers is detecting change after init without changing object

I have a form for which I've got a js object as model. 我有一个表格,我有一个js对象作为模型。 I want to detect a change in the object, to inform the user of the need to save. 我想检测对象的变化,通知用户需要保存。

To achieve this, I currently using KeyValueDiffers . 为实现这一目标,我目前正在使用KeyValueDiffers It does also detect every change, as wanted. 它还可以根据需要检测每个更改。 Although I have an unwanted effect. 虽然我有不良影响。

The form is an own component and I'll set the object to modify over an @Input() which calls a init() method that also initializes the KeyValueDiffers with the new object. 表单是一个自己的组件,我将设置对象来修改@Input() ,它调用一个init()方法,该方法也用新对象初始化KeyValueDiffers

After initialize the KeyValueDiffers I immediately get a change notification, although there was no change to the object that I used to initialize the differ yet. 初始化KeyValueDiffers我立即收到更改通知,尽管我用来初始化差异的对象没有变化。 Why could this be? 为什么会这样? I'm clueless... 我很无能......

export class MetadataDetailComponent implements DoCheck {

    constructor(private differs: KeyValueDiffers) { }

    unsavedChanges: boolean = false;
    differ: any;
    metadata: Metadata;
    orgMetadata: Metadata;

    @Input()
    set setMetadata(metadata: Metadata) {
        this.init(metadata);
    }

    private init(metadata: Metadata) {
        this.differ = undefined;
        if (this.metadata && this.unsavedChanges) {
            console.log('unsaved changes!!')

        }
        //Do different init stuff
        this.orgMetadata = metadata;
        this.metadata = new Metadata(metadata.name, metadata.description, metadata.id, metadata.state, metadata.type, metadata.script);
        this.differ = this.differs.find(this.orgMetadata).create(null);
        this.unsavedChanges = false;
    }

    ngDoCheck() {
        if (this.differ) {
            var changes = this.differ.diff(this.metadata);
            if (changes) {
                this.unsavedChanges = true;
                console.log('metadata has changed!');
            }
        }
    }
}

EDIT 编辑

This is the change event I'm getting 这是我得到的改变事件 在此输入图像描述

It seems like this is the change from null to the object. 看起来这是从null到对象的变化。 But why am I getting this change when I'm already initializing the KeyValueDiffers with the object? 但是,当我已经使用对象初始化KeyValueDiffers时,为什么我会得到此更改?

I don't know if this solves your specific problem. 我不知道这是否能解决您的具体问题。 But we had issues with KeyValueDiffer having "null" as the previous value on the first diff after things get going after ngOnInit. 但是我们遇到的问题是KeyValueDiffer在ngOnInit之后的事情发生后,第一个差异上的前一个值为“null”。 I've combed the internet and found no real answer on this, so we "solved" it by initializing the differ like so 我已经梳理了互联网,并没有找到真正的答案,所以我们通过初始化差异来“解决”它

this.differ = this.differs.find(this.orgMetadata).create().diff(this.orgMetadata);

or perhaps for your specific case 或者也许是针对您的具体情况

this.differ = this.differs.find(this.orgMetadata).create().diff(this.metadata);

Note that create doesn't take an argument anymore in Angular 6+ 请注意,在Angular 6+中,create不再使用参数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM