2

I want to use a directive that have an attribute. I want this attribute to always be true, so I do not need a scope variable for this. But I am pretty sure that the following is wrong, it is probably a scope variable named true instead of the boolean value true. How should this write what I want?

<accordion close-others="true">
</accordion>

Edit: I realized that this was a bad example. What if I have an attribute which I want to give the value "someText". How can I make the difference between the value "someText" and some variable named "someText"? Like this:

<some-directive some-attribute="someText"></some-directive>
7
  • should work fine the way you have it, if not create demo that isn't doing as expected Commented Sep 6, 2014 at 14:16
  • How can I change between using the boolean value true and and a variable named true? Does Angular first check if there is a variable named true and if not then treat it as a value? Commented Sep 6, 2014 at 14:21
  • Oh, I just realized, it is probably not allowed to call a variable "true"... Sorry for the stupid question! Commented Sep 6, 2014 at 14:23
  • I updated the question with a less stupid variant... Commented Sep 6, 2014 at 14:45
  • did you try using single quotes to make it a string? Since html is showing a boolean, question is less clear now with edit Commented Sep 6, 2014 at 14:48

1 Answer 1

3

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>
Sign up to request clarification or add additional context in comments.

1 Comment

Wow, this was a much more detailed than I was expecting, and an answer that I learned a lot from! Thanks a lot @MArc :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.