I created a JSFiddle to illustrate my situation: http://jsfiddle.net/hLv27/
I have a set of directives that should be independent one from the other but update depending on the data provided by an attribute (topics). I can exemplify this by using the same directive multiple times:
<my-directive topics="main.topicList"></my-directive>
<my-directive topics="main.staticTopicList"></my-directive>
<my-directive topics="main.topicList" update-on="some.event"></my-directive>
Each directive fetches its data from the server, depending on the topics:
in the first case, I would use a
$watch, observe thetopicsarray, and fetch new data accordingly;$scope.$watch("topics", function (theTopics) { $scope.fetchData(theTopics); });in the second case, the directive is initialised with a fixed array and I do not need to update it afterwards;
in the third case, I would add to each directive a
update-onstring corresponding to a broadcast that gets fired when thetopicsarray changes.if ($scope.updateOn) { $scope.$on($scope.updateOn, function (ev, tags) { $scope.fetchData(tags); }); }
The question is: would it be better to use $watch or $broadcast?
Addendum: is there a better way to create similar directives? (e.g. without having to watch or broadcast)