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

    }

相关文章

ANGULAR.JS:NG-SELECTANDNG-OPTIONSPS:其实看英文文档比看中...
AngularJS中使用Chart.js制折线图与饼图实例  Chart.js 是...
IE浏览器兼容性后续前言 继续尝试解决IE浏览器兼容性问题,...
Angular实现下拉菜单多选写这篇文章时,引用文章地址如下:h...
在AngularJS应用中集成科大讯飞语音输入功能前言 根据项目...
Angular数据更新不及时问题探讨前言 在修复控制角标正确变...