Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angularjs dynamically set attribute

I'm trying to dynamically add attribute to div in controller in angular js.

 var table = document.getElementById("div_id").setAttribute("ng-click", "function_name()");  $scope.$apply(); 

Everything looks fine, in the debugger i see that attribute was added but it doesn't execute my function. Do you have any ideas how to add attributes to the existing div and how to make it works?

like image 819
user2282428 Avatar asked Sep 10 '14 07:09

user2282428


People also ask

What is Ng ATTR?

ng-attr is used for add or not the attribute in context. If the expression {{undefined || null}} , the attribute is not added otherwise if has a value then attribute is added with the value {{ value }} . The most common cases is in interpolation.

What is an angular element?

Angular elements are Angular components packaged as custom elements (also called Web Components), a web standard for defining new HTML elements in a framework-agnostic way.


1 Answers

You need to recompile your div

var el = angular.element("div_id"); $scope = el.scope(); $injector = el.injector(); $injector.invoke(function($compile){    $compile(el)($scope) }) 

http://jsfiddle.net/r2vb1ahy/

like image 131
cevek Avatar answered Oct 14 '22 13:10

cevek