2017-08-03 63 views
0

我想在Angular 4中使用isPropertyUpdated,但是Angular 4不支持深导入。角4导入isPropertyUpdated方法

所以这个进口不会对角4工作:

import {isPropertyUpdated} from 'angular2/src/common/forms/directives/shared' 

所以我能做些什么,对角4使用isPropertyUpdated

+0

这种方法是不公开的。从内部包导入它是有风险的 –

+0

要检测属性更改,请参阅今天早些时候@GünterZöchbauer的答案:https://stackoverflow.com/a/45477825/1791913 – Faisal

+0

该死,我无法访问this._state控制器,使用我的如果这样,将解决我的问题:if(isPropertyUpdated(changes,this.lastViewModel)){} –

回答

0

如果你真的需要这个,你可以阅读source,并决定这是否是你所需要的矫枉过正。我认为ngOnChanges()将适合大多数需求。

在你的项目中添加这样的事情:所以它不是在`@角/ forms`封装体露出

import { elooseIdentical } from '@angular/core'; 

export function isPropertyUpdated(changes: {[key: string]: any}, viewModel: any): boolean { 
    if (!changes.hasOwnProperty('model')) return false; 
    const change = changes['model']; 

    if (change.isFirstChange()) return true; 
    return !elooseIdentical(viewModel, change.currentValue); 
} 
+0

我只用于验证,如下所示:ngOnChanges(changes){ \t \t if isPropertyUpdated(变化,this.lastViewModel)){ \t \t \t this.lastViewModel = this.model \t \t \t this.refreshView() \t \t} \t} –