Let's say I have 2 directives: "parent" (i.e. table) and "child" (tr), I would like to be able to select child elements and keep the list of them in my parent directive (I think the controller is the only possible place, isn't it?). I'm handling click events to update the list stored in my controller and it works fine but I'd also like to keep "selected" class for selected items, but because the selection may also be changed by the controller of parent directive (and modifying the DOM in controller is "forbidden"), the only possibility I could think of is a watcher in my child directive.
Take a look at the example, please:
angular.module('App')
.directive('parent', function () {
return {
scope : {},
controller: function($scope) {
this.selectedItems = [];
}
}
}).directive('child', function() {
return {
require: '^parent',
link: function(scope, element, attrs, controller) {
scope.$watchCollection('controller.selectedItems', function() {
//add or remove 'selected' class here.
}
}
}
});
That doesn't work (the watcher event fires only when the array reference changes and not individual elements) and it doesn't seem to be designed correctly either, could you provide me with a better approach to that issue?
update:
Notice that controller.selectedItems
is modified in the controller, however the child directive does not seems to watch any change.