AngularJS-正在检索已评估的属性
AngularJS - retrieving evaluated attribute
我有一个基本的表单输入指令,它根据名称设置一些表单元素:
angular.module('myApp').directive('formInput', function () {
return {
restrict: 'A',
require: '^form',
link: function (scope, element, attributes, form) {
var input, name;
input = element[0].querySelector('input, textarea, select');
name = input.getAttribute('name');
// ...
// do stuff with input, name, form etc.
}
};
});
在我的HTML中,我做了一些简单的DOM设置,它就完成了任务。
<div form-input>
<input type="text" name="myElement"/>
</div>
当我开始使用动态名称时,问题就来了,即
<div form-input>
<input type="text" name="{{ getDynamicName(element) }}"/>
</div>
在进入我的指令之前,不会评估动态名称。有办法解决这个问题吗?
附言:考虑到指令的装饰性,我不喜欢使用隔离范围。
使用0ms的$timeout
服务在内部元素链接后运行代码:
// Note that $timeout is now injected to the directive
angular.module('myApp', []).directive('formInput', function ($timeout) {
return {
restrict: 'A',
//require: '^form',
link: function (scope, element, attributes, form) {
$timeout(function() {
var input, name;
input = element[0].querySelector('input, textarea, select');
name = input.getAttribute('name');
alert(name);
}, 0);
// ...
// do stuff with input, name, form etc.
}
};
});
JSFiddle
相关文章:
- RegEx删除空属性?例如,如果(class=“”||class=“”)移除;否则就下课
- 全局变量和全局对象的属性之间有什么区别吗
- 如果使用 lodash 将属性存在于另一个对象中,则向对象添加属性
- 如何在Bootstrap Modal中为动态点击生成的变量设置jade属性
- JavaScript:获取对象的评估属性
- JavaScript 评估 - 如何为对象属性赋值
- ASP WebForms在返回后重新评估属性
- 等待评估的属性
- 如何评估潜在未声明变量的属性
- 具有数据绑定URL的ExtJS存储未重新评估数据属性
- 在JavaScript中定义的Angular.js模型中,需要将一个属性作为函数进行评估
- 子属性更改时重新评估对象绑定
- Thymelaf评估Javascript中的嵌套属性
- AngularJS:NgShow评估集合中的对象属性
- AngularJS-正在检索已评估的属性
- 为什么'无法读取属性'x'未定义的'未评估为false
- 如何引用和评估变量值作为对象属性
- Phantomjs改变对象属性内页评估
- 评估HTML5数据属性-如何处理引号
- 试图测试元素是否具有属性而不直接评估元素