我正在使用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": "[email protected]",
"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": "[email protected]",
"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初始水合时获得了预期的输出,但是在调用merge函数之后却没有得到。数据正确更改,但是$ watch方法永远不会被命中。
是什么赋予了?
这是一个矮胖的人(在咖啡脚本中),以显示完整的工作流程:http ://plnkr.co/edit/VPnxhUa6uz1zKfcufb2J
$ watch函数接受第三个布尔参数,该参数指示比较对象是否相等或进行引用:
$scope.$watch('bigDocument', function(newVal, oldVal) {
console.log('bigDocument Changed');
}, true);
这个pl子工作正常。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句