[Edited to reflect comments in @shaunhusain answer]
I have a nested ng-repeat construct for which I want to instantiate a new instance of a directive for every inner item, passing to that directive the data from that inner item. In the code below, I've included two nested loops. The one that works uses one-way binding via the {{ }} notation, and the other appears to work as well...until you hit the Refresh button. Even though $scope.frames changes (and the {{ }} binding continues to reflect the changes), the binding for the directive is not triggered.
What am I missing?
var myApp = angular.module('myApp', []);
myApp.directive("myDirective", function () {
return {
restrict: 'E',
scope: {
boundData: '=data'
},
link: function (scope, element) {
angular.element(element.find("div")[0])
.html('')
.append("<p>" + scope.boundData.val + "</p>");
}
}
});
myApp.controller('FooCtrl', ['$scope', function ($scope) {
$scope.clear = function () {
$(".item-list").empty();
};
$scope.getData = function () {
var frames = [];
for (var i = 0; i < 2; i++) {
var items = [];
for (var j = 0; j < 4; j++) {
items.push({ val : Math.floor(Math.random() * 5000) });
};
frames.push(items);
}
$scope.frames = frames;
};
$scope.getData();
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div ng-app="myApp">
<div ng-controller="FooCtrl">
<div>
<button ng-click="getData()">Refresh</button>
<button ng-click="clear()">Clear</button>
</div>
<div style="float: left">
<b>{} binding</b>
<div ng-repeat="frame in frames track by $index">
<div ng-repeat="item in frame track by $index">
{{item.val}}
</div>
</div>
</div>
<div style="margin-left: 120px">
<b>Using directive</b>
<div ng-repeat="frame in frames track by $index">
<div ng-repeat="item in frame track by $index">
<my-directive data="item">
<div class="item-list"></div>
</my-directive>
</div>
</div>
</div>
</div>
</div>