2016-09-23 48 views
0

所以說你有下面的代碼:Angularjs應用更新對象

var absenceType = {name: 'hello'}; 
this.newAbsenceType = angular.copy(absenceType); 

現在您更改this.newAbsenceType,並希望將這些更改應用到原始對象。

所以我一直都在擴大:

angular.extend(absenceType, this.newAbsenceType); 

但是這並沒有這樣的伎倆。

我在做什麼錯了?

+0

'angular.copy'回報_ 「複製或更新的目的地,如果指定的目的地」 _ – zeroflagL

+0

'angular.extend(absenceType,this.newAbsenceType)'應該工作。如果沒有,那麼你做錯了,你沒有在問題中顯示。 – dfsq

回答

1

使用合併

angular.merge(absenceType, this.newAbsenceType); 

同時也延長了應工作方式相同它們之間唯一的區別是深深合併擴展了目標對象。

演示: https://jsbin.com/jucebix/9/edit?html,js,console

+0

是從1.4以上的角度合併功能? –

+0

是的! https://code.angularjs.org/1.4.12/docs/api/ng/function/angular.merge – KrishCdbry

0

您可以使用$範圍。$看

$scope.$watch(function(){ return this.newAbsenceType}, function(newValue, oldValue) { 
    //do change 
});