I'm attempting to trigger a $watch after a timeout in angular:
My Controller
.controller('MyCtrl', ['$scope', function ($scope) {
$scope.chickens = ["Jim", "Joe", "Fred"];
$scope.chickens.push("Steve");
setTimeout(function () {
$scope.chickens.push("Larry");
$scope.$apply();
}, 500);
}])
Then the $watch is wired up in my directive's link function:
My Directive Link
link: function (scope, element, attrs) {
scope.$watch('chickens', function (newChickens, oldChickens) {
$(element).html('');
newChickens.forEach(function (chicken) {
$(element).append($('<li/>', {
'text': chicken
}));
});
});
}
The $watch only seems to fire once with the first four elements, it never fires after the fifth is added. I tried adding the $scope.$apply() there to apply the changes. Doesn't seem to be working.
Here's the Fiddle for the example. I'd appreciate any help in the situation. Thanks!