I'm merging a smaller object into a larger object using angular.extend.
Here's a sample of data:
$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,
}
I'm using the following function to merge:
$scope.merge = function() {
angular.extend($scope.bigDocument, $scope.littleDocument)
and watching for changed with the following
$scope.$watch('bigDocument', function(newVal, oldVal) {
console.log('bigDocument Changed');
})
I get the expected output on initial hydration of $scope.bigDocument, but not after the merge function is called. The data changes correctly, but the $watch method never gets hit.
What gives?
Here's a plunker (in coffeescript) to show the full workflow: http://plnkr.co/edit/VPnxhUa6uz1zKfcufb2J
The $watch function accepts a third boolean parameter indicating either to compare object for equality or reference:
$scope.$watch('bigDocument', function(newVal, oldVal) {
console.log('bigDocument Changed');
}, true);
This plunker works as expected.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With