首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用角范围指令变量内容作为属性本身

使用角范围指令变量内容作为属性本身
EN

Stack Overflow用户
提问于 2016-06-17 13:12:29
回答 2查看 273关注 0票数 1

我在任何地方都找不到这个问题的答案,所以我要在这里问。是否有任何方法使用角范围指令变量内容作为属性本身?

例如:

查看输入:

代码语言:javascript
复制
<custom-directive
    attr-one="Atribute value 1"
    ng-model="cool.model"
    message="Message 1"
    extra-attr="variable-attribute"
></custom-directive>

指令文件:

代码语言:javascript
复制
app.directive('customDirective', [
    function () {
        return {
            restrict: 'E',
            templateUrl: createuri('/templates/custom-directive'),
            require: 'ngModel',
            scope: {
                message: '@message',
                ngModel: '=ngModel',
                extraAttr: '@extraAttr',
                attrOne '@attrOne'
           }
        }
    }
]);

指令模板文件:

代码语言:javascript
复制
<input type="text"
    attr-one="attrOne"
    class="input-directive"
    ng-model="ngModel"
    message="message"
    {{extraAttr}} %{--something like this--}%
/>

在某种程度上,输出将以这样的方式结束:

代码语言:javascript
复制
<input type="text"
    attr-one="Atribute value 1"
    class="input-directive"
    ng-model="cool.model"
    message="Message 1"
    variable-attribute
/>

编辑:我不确定这是赋值错误,因为当我尝试使用一个正在工作的变量({{label}})时,例如,这就是我得到的:

变量在元素的内容区域中输出,而在元素属性定义区域中不输出。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-06-17 14:27:16

最后,我找到了答案,它包括在链接之前运行时使用编译指令函数。

代码语言:javascript
复制
/*[...]*/
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 */
}
/*[...]*/
票数 0
EN

Stack Overflow用户

发布于 2016-06-17 14:16:39

正如这里所说:"Web浏览器有时会挑剔它们认为哪些值对属性有效。“

为此尝试使用ngAttr:

ng-attr-标签=“{{yourLabelValue}”

标签可以替换为任何属性名称,如ng-attr-variable-attribute="{{attributeValue}}“,表示”变量-属性“。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37882495

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档