Javascript AngularJS-检索已计算属性
我有一个基本表单输入指令,它根据名称设置一些表单元素:Javascript AngularJS-检索已计算属性,javascript,angularjs,angularjs-directive,Javascript,Angularjs,Angularjs Directive,我有一个基本表单输入指令,它根据名称设置一些表单元素: angular.module('myApp').directive('formInput', function () { return { restrict: 'A', require: '^form', link: function (scope, element, attributes, form) { var input, name; input = element[0].query
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>
在进入我的指令之前,不计算动态名称。有没有办法解决这个问题
另外,鉴于指令的装饰性,我不喜欢使用隔离作用域。在链接内部元素后,使用
$timeout
服务0ms运行代码:
// 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.
}
};
});