Attribute without value in AngularJS directive

我与影子孤独终老i 提交于 2019-12-05 08:13:07

Just use attrs.hasOwnProperty('noValueAttr') in the link function to test whether the attribute is present or not.

Don't forget the attribute in markup would be no-value-attr, not noValueAttr like you showed.

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

    }

I know it is an old question, but there is that way to:

link: function(scope, elem, attrs) {
  scope.noValueAttr = scope.$eval(attrs.noValueAttr) || 'default value';
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!