0

Given:

 scope:{
      suit: '@',
      value: '@'
    },

I can access these values in my directive template with {{suit}} or {{value}} I can also access them in my link function with attr.suit or attr.value.

What happens when I declare class = "{{suit}} {{value}}" in the template and also do elem.addClass(attr.suit) and elem.addClass(attr.value) at the same time?

It seems that the {{}} value takes precedence and overrides what I do in the link function. Can anyone explain why that is? Also, what is the better practice?

1
  • Put them in scope if needed by the directive template, and if the view does not need them then use attr. Commented May 1, 2014 at 21:51

1 Answer 1

1

At least one consideration is if you expect the values to change in the same directve. Can 'suit' value can be x, and later be changed to y?

  1. If you expect the 'suit' or 'values' to change, considering you're not manipulating them, I'd use them in the template.

ng-class="{{suit}} {{value}}"

(ng-class has better support for interpolations)

  1. If you don't expect 'suit' or 'values' to change, you can remove them as an isolated scope variables since it defines a binding that you don't need, and use elem.addClass(yourAttribute) as you've already written.
Sign up to request clarification or add additional context in comments.

Comments

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.