You define in your directive how angular should handle that attribute value.
This means it will be parsed as regular javascript if you define it like this:
scope: {
closeOthers: '='
}
//gives you in scope.closeOthers:
<e close-others="true" /> //true
<e close-others="'someText'" /> //someText
<e close-others="variableName" /> //contentOfVariableName
What you actually want is to parse it as string attribute (string with embedded interpolation expressions) like this:
scope: {
closeOthers: '@'
}
//gives you in scope.closeOthers:
<e close-others="true" /> //true
<e close-others="'someText'" /> //'someText'
<e close-others="variableName" /> //variableName
<e close-others="prefix/{{variableName}}" /> //prefix/contentOfVariableName
scope: {}creates a new isolated scope. I don't personally like those isolated scopes and prefer to create a normal sub scope with scope: true and watch or parse attribute values on my own:
.directive('e', ['$interpolate', function($interpolate) {
return {
restrict: 'E',
scope: true,
link: function(scope, element, attributes) {
attributes.$observe('closeOthers', function(value) {
if ('true' === value.toLowerCase()) {
// ...
}
});
//or if you don't need a watcher, which I prefer sometimes, because it's not always needed and costs performance
var value = $interpolate(attributes.closeOthers)(scope.$parent);
if ('true' === value.toLowerCase()) {
///...
}
}
}
});
You said I want this attribute to always be true, so I do not need a scope variable for this, so I guess you don't even need to parse it (if you don't care about {{}} expressions). Then you can simply do:
link: function(scope, element, attributes) {
var closeOthers = 'closeOthers' in attributes; //true or false
}
Which allows you to use 'close-others' like this:
<accordion close-others></accordion>
<accordion close-others="true"></accordion>