javascript - Angularjs动态设置属性

标签 javascript angularjs attributes setattribute

我正在尝试在 Angular JS 中动态地将属性添加到 Controller 中的 div 中。

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

一切看起来都很好,在调试器中我看到该属性已添加,但它不执行我的函数。您对如何向现有 div 添加属性以及如何使其发挥作用有什么想法吗?

最佳答案

您需要重新编译您的 div

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

http://jsfiddle.net/r2vb1ahy/

关于javascript - Angularjs动态设置属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25759497/

相关文章:

javascript - 在 Firefox 中动态加载 JQuery 时不稳定

javascript - AngularJS + Jasmine + karma : service response mocking in controller

attributes - Nunit Action 属性构造函数被多次调用

java - 如何仅显示 thymeleaf 中不为空的变量

c# - 重写属性上的 .net XmlSerializer

javascript - Object.defineProperty 有时会抛出异常

javascript - Codeigniter 和 Jquery Ajax,发布数据未通过

javascript - 迭代嵌套对象 - 如果字段匹配变量,则更新 $scope

angularjs - 将 cordova 插件 paypal 与 ionic 集成

javascript - 事件处理函数不适用于 `this` 关键字