In my plunk I have an array of integers, that I have attached $scope.$watch to. When I update the array, my $scope.$watch isnt firing a console.log. Why is my console.log not being called?
<!DOCTYPE html>
<html>
<head>
<script data-require="[email protected]" data-semver="1.5.0" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.2/angular.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head>
<body ng-app="test" ng-controller="testArray">
<div ng-repeat="item in MyArray track by $index"> {{item}}</div>
<button ng-click="add()">testing Add</button>
</body>
</html>
<script type="text/javascript">
'use strict';
var app = angular.module('test', []);
app.controller('testArray',['$scope',function($scope){
$scope.MyArray = [1,2,3,4]
$scope.add = function(){
$scope.MyArray.push($scope.MyArray.length+1);
}
$scope.$watch('MyArray' , function(){
console.log($scope.MyArray);
})
}]);
</script>