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 } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |