Created
March 31, 2014 12:54
-
-
Save sz-alpar/9ba60fde72b677244b02 to your computer and use it in GitHub Desktop.
AngularJS watch array length change.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | |
} | |
}); | |
}]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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