微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

AngularJS指令中没有值的属性

我写了一个带隔离范围的指令.
app.directive('myDirective',function() {
    return {
        restrict: 'E',scope {
            attr1: '@',attr2: '@',novalueAttr: // what to put here?
        },link: function(scope,elem,attrs) {
            // how to check here if novalueAttr is present in mark-up?
        }
    };
});

HTML可能是

<my-directive attr1='...' attr='...' ... no-value-attr>

要么

<my-directive attr1='...' attr='...' >

我想知道如何使用(并使指令检测它是否存在)一个没有赋值的可选属性.谢谢.

只需在链接函数中使用attrs.hasOwnProperty(‘novalueAttr’)来测试属性是否存在.

不要忘记标记中的属性是no-value-attr,而不是像你所示的novalueAttr.

link: function(scope,attrs) {
           if (attrs.hasOwnProperty('novalueAttr'))
              // attribute is present
           else 
              // attribute is not present

    }

原文地址:https://www.jb51.cc/angularjs/141042.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐