Reputation: 21304
I'm just wondering is it a good practice to make http GET request to fetch data inside an angular directive? Directive's behavior is dependent on that data.
So the main complexity is to get it before it's compiled (to add special classes to elements for example).
Thanks in advance!
Upvotes: 0
Views: 428
Reputation: 21304
It's important to add that if you use $watch
listening $resource
you should add true
at the end:
scope.$watch('NeededDataLoadedFromServer', function(newval, oldval) {}, true);
Upvotes: 0
Reputation: 29538
In your controller, create a model that holds all needed data for your directive. Create a directive and use ng-model
atribute in the markup to bind the model to the directive. Then in your directive's link
method, watch any changes in this model using the $scope.$watch method. Here is a good example integrating a flot chart with angularjs: http://jsfiddle.net/TDwGF/3/
Upvotes: 1
Reputation: 30015
IMHO directives should consume the model, not get, post or manipulate the model. I would make an attribute in your directive called 'src' or similar that would allow your directive to bind to a controller scope. Then do the get in your controller. Have that get update $scope and therefore your directive as well.
Upvotes: 2