Reputation: 7951
I've written a directive with an isolate scope.
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?
}
};
});
the html could be
<my-directive attr1='...' attr='...' ... no-value-attr>
or
<my-directive attr1='...' attr='...' >
I am wondering how to use (and have the directive detect if it's there or not) an optional attribute which has no assigned value. Thanks.
Upvotes: 7
Views: 6145
Reputation: 29
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';
}
Upvotes: 0
Reputation: 38121
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
}
Upvotes: 22