Skip to content

Instantly share code, notes, and snippets.

@sz-alpar
Created March 31, 2014 12:54
Show Gist options
  • Save sz-alpar/9ba60fde72b677244b02 to your computer and use it in GitHub Desktop.
Save sz-alpar/9ba60fde72b677244b02 to your computer and use it in GitHub Desktop.
AngularJS watch array length change.
angular.module('myApp', []);
angular.module('myApp').controller('watcherCtrl', ['$scope', function($scope) {
$scope.myArray = [];
var index = 42;
$scope.push = function () {
$scope.myArray.push(index++);
}
$scope.$watch('myArray.length', function() {
if ($scope.myArray.length > 0) {
alert('Index pushed into myArray');
}
});
}]);
<div ng-app='myApp'>
<div ng-controller='watcherCtrl'>
<input type='button' title='Push' ng-click='push()'/>
<ul>
<li ng-repeat='item in myArray'>{{item}}</li>
</ul>
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment