I am using a directive that injects a service. When the data changes from the service I would like the directive to update.
I know I need to use $watch but I am not sure how to implement it in my situation.
I have tried a couple of scenarios but they have not worked. Below is my directive.
Can someone please show me how to add in the $watch so the directive updates when the data changes?
app.directive('googleAnalytics', function(configFactory){
return {
restrict: 'E',
replace: true,
link: function(scope,element,attrs){
configFactory.getconfigs().then(function(configs) {
scope.gid = configs[0].ga_id;
var scriptTag = angular.element(document.createElement("script"));
scriptTag.text("ga('create', '"+scope.gid+"', 'auto');")
element.append(scriptTag);
});
}
};
})