3

我正在使用 angular.extend 将较小的对象合并到较大的对象中。

这是一个数据示例:

$scope.bigDocument = {
    "id": 0,
    "guid": "e75ce3b3-68f6-423f-94d8-1a613cde0c59",
    "isActive": true,
    "balance": "$2,437.00",
    "picture": "http://placehold.it/32x32",
    "age": 38,
    "name": "Parks Williamson",
    "gender": "male",
    "company": "Arctiq",
    "email": "parkswilliamson@arctiq.com",
    "phone": "+1 (817) 488-3119",
    "address": "252 Concord Street, Morgandale, Arizona, 1866",
    "about": "Irure nostrud nisi qui do amet nisi adipisicing dolor eiusmod do non laboris.",
    "registered": "1995-04-08T20:16:47 +05:00",
    "latitude": -6.796341,
    "longitude": 40.321499,
    "randomArrayItem": "cherry"
}

$scope.littleDocument = {
     "balance": "$3,193.00",
     "picture": "http://placehold.it/32x32",
     "age": 22, 
     "name": "Coffey Wilcox",
     "gender": "male",
     "company": "Balooba",
     "email": "coffeywilcox@balooba.com",
     "phone": "+1 (824) 518-3639",
     "address": "836 Douglass Street, Imperial, Montana, 9365",
     "about": "Ipsum dolore officia consectetur proident occaecat.",
     "registered": "1994-12-12T15:47:09 +06:00",
     "latitude": -33.474425,
     "longitude": -113.998081,
}

我正在使用以下函数进行合并:

$scope.merge = function() {
    angular.extend($scope.bigDocument, $scope.littleDocument)

并注意以下内容的更改

$scope.$watch('bigDocument', function(newVal, oldVal) {
    console.log('bigDocument Changed');
})

我在 $scope.bigDocument 的初始水合中得到了预期的输出,但在调用合并函数之后没有。数据正确更改,但 $watch 方法永远不会被命中。

是什么赋予了?

这是一个显示完整工作流程的 plunker(在 coffeescript 中):http: //plnkr.co/edit/VPnxhUa6uz1zKfcufb2J

4

1 回答 1

5

$watch 函数接受第三个布尔参数,指示比较对象是否相等或引用:

$scope.$watch('bigDocument', function(newVal, oldVal) {
    console.log('bigDocument Changed');
}, true);

这个plunker按预期工作。

于 2013-11-08T15:35:46.343 回答