AngularJS - 在运行时添加属性 [英] AngularJS - Add attribute at runtime

查看:130
本文介绍了AngularJS - 在运行时添加属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个AngularJS应用。我试图自动对焦属性在运行时添加到一个元素。我想这样做的原因是这样我就可以在几个元素之一设置该属性。如何通过一个AngularJS指令添加一个HTML属性的元素?

I'm working on an AngularJS app. I'm trying to add the autofocus attribute to an element at runtime. The reason I want to do this is so I can set this attribute on one of several elements. How do you add an HTML attribute to an element via an AngularJS directive?

感谢您!

推荐答案

您可以使用 $ compile.directive.Attributes 对象的$ set方法。查看文档的 rel=\"nofollow\">。这将创建一个AngularJS会识别新的属性。记住使用属性的规范化(驼峰)版本。你可以在你的指令的链接功能。

You can use the $set method on the $compile.directive.Attributes object. See the documentation here. This will create a new attribute which AngularJS will recognize. Remember to use the normalized (camelCase) version of the attribute. You can do it in the link function of your directive.

这篇关于AngularJS - 在运行时添加属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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