It would be best to make this change inside the directive. If for some reason this is not possible, then there are several options.
Outside the application, get a link to any DOM element in the application. Using this link, you can get a link to its area. You can use your element with id d1 . For instance:
var domElement = document.getElementById('d1'); var scope = angular.element(domElement).scope();
Here are a few options:
Option 1
Change the model instead of directly changing the view. In the communication function, save the initial value of the attribute in the scope variable, for example:
scope.myvalue = attrs.attr1;
Then you can change the value outside the application (using the scope link above), for example:
scope.$apply(function(){ scope.myvalue = 1000; console.log('attribute changed'); });
Here is the fiddle
Option 2
If a view is directly processed using jQuery, I don’t know how to use $observe , $watch or binding the selection to an attribute that will work, because they all bind to the attribute expression itself, only once, when the link function is first run. Changing the value will cause these bindings to fail. Thus, you will have the $watch attribute of the DOM element itself (and not through attrs ):
scope.$watch(function(){ return $(el).attr('attr1');
Then you can change the value outside the application (using the scope link above), for example:
scope.$apply(function(){ $("#d1").attr("attr1",1000); });
Here is the fiddle
source share