我在任何地方都找不到这个问题的答案,所以我要在这里问。是否有任何方法使用角范围指令变量内容作为属性本身?
例如:
查看输入:
<custom-directive
attr-one="Atribute value 1"
ng-model="cool.model"
message="Message 1"
extra-attr="variable-attribute"
></custom-directive>指令文件:
app.directive('customDirective', [
function () {
return {
restrict: 'E',
templateUrl: createuri('/templates/custom-directive'),
require: 'ngModel',
scope: {
message: '@message',
ngModel: '=ngModel',
extraAttr: '@extraAttr',
attrOne '@attrOne'
}
}
}
]);指令模板文件:
<input type="text"
attr-one="attrOne"
class="input-directive"
ng-model="ngModel"
message="message"
{{extraAttr}} %{--something like this--}%
/>在某种程度上,输出将以这样的方式结束:
<input type="text"
attr-one="Atribute value 1"
class="input-directive"
ng-model="cool.model"
message="Message 1"
variable-attribute
/>编辑:我不确定这是赋值错误,因为当我尝试使用一个正在工作的变量({{label}})时,例如,这就是我得到的:

变量在元素的内容区域中输出,而在元素属性定义区域中不输出。
发布于 2016-06-17 14:27:16
最后,我找到了答案,它包括在链接之前运行时使用编译指令函数。
/*[...]*/
priority: 1001,
terminal: true,
compile: function(el, attr) {
var ipt = el[0].childNodes[0].childNodes[1];
/* ^ searching for the element I want to bind the directive attribute to*/
ipt.setAttribute(attr.extraAttr, '');
/* ^ setting the attribute to the value contained in extraAttr */
}
/*[...]*/发布于 2016-06-17 14:16:39
正如这里所说:"Web浏览器有时会挑剔它们认为哪些值对属性有效。“
ng-attr-标签=“{{yourLabelValue}”
标签可以替换为任何属性名称,如ng-attr-variable-attribute="{{attributeValue}}“,表示”变量-属性“。
https://stackoverflow.com/questions/37882495
复制相似问题