加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

AngularJS指令中没有值的属性

发布时间:2020-12-17 08:50:55 所属栏目:安全 来源:网络整理
导读:我写了一个带隔离范围的指令. 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
我写了一个带隔离范围的指令.
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

    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读