Angularjs正在对对象进行更新



假设你有以下代码:

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

现在您对this.newAbsenceType进行了更改,并且希望将这些更改应用于原始对象。

我们一直在看extend:

angular.extend( absenceType, this.newAbsenceType);

然而,这并没有达到目的。

我做错了什么?

使用合并

 angular.merge(absenceType, this.newAbsenceType);

但是extend也应该以同样的方式工作,它们之间唯一的区别是merge深度扩展目标对象。

演示:https://jsbin.com/jucebix/9/edit?html, js、控制台

你可以使用$scope.$watch

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

最新更新