I am using $watch
for listening to myData
which is an array. In a button click I am inserting a new element into the array. I need to find the position of the newly added element from the newValue
array ,which is a parameter in $watch
.
$scope.myData = [{
"name": "karthik ",
"age": 24
},
{
"name": "Vijay ",
"age": 24,
},
{
"name": "Krish",
"age": 26,
}];
var newData = {
"name": "viki",
"age": 25
}
myData.splice(2, 0, newData);
In the link function I have
scope.$watchCollection(function(scope) {
return scope.myData;
}, function(newVal, oldVal) {
console.log(newVal);
console.log(oldVal);
});