Angularjs 动态设置属性 [英] Angularjs dynamically set attribute

查看:26
本文介绍了Angularjs 动态设置属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 angular js 中为控制器中的 div 动态添加属性.

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();

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

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?

推荐答案

你需要重新编译你的div

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/

这篇关于Angularjs 动态设置属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆