AngularJS - directive ng-repeat 14388247
by mrajcok
HTML
<div ng-app="myApp">
<div ng-controller="ctrl">
<ul mylist>
<li ng-repeat="myItem in items">
<span class="etc">{{myItem}}</span>
</li>
</ul>
<a ng-click="changeItem0()">change item 0</a>
</div>
</div>
JavaScript
var myApp = angular.module('myApp', [])
.controller('ctrl', function($scope) {
$scope.items = ['one', 'two', 'three'];
$scope.changeItem0 = function() {
$scope.items[0] = 1;
}
})
.directive('mylist', function() {
return {
scope: true,
link: function(scope, element, attr) {
scope.$watch('items', function(value) {
alert('watch fired, item[0]=' + value[0]);
console.log(value[0])
}, true);
}
}
});