2017-08-03 64 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} –