0

I have a bunch of attributes that pertain to a specific functionality. They are quite a pain to write so I decided that it would be best to write it as a directive.

The problem is there doesn't seem to be a simple way to add attributes in a directive. For example, in this question, to add the directive compile has to still be called, and it's using the attr method of jqlite.

And that is the problem: directives as a usable component, how would I add attributes painlessly?

For example, if I want to add an ng-class, it's not going to be easy because there would have to be string manipulation ('{myClass:foo}' + '{directiveClass:bar}', to {myClass:foo, directiveClass:bar});

Or is it a bad idea to add attributes to directives? The problem is I can't add the attributes in a template because it must be attributes to the actual element (because it might break styles, etc.))

Community
  • 1
  • 1
Secret
  • 3,291
  • 3
  • 32
  • 50
  • 1
    I think a lot of people bumped into that problem ie http://stackoverflow.com/questions/22116470/add-ng-click-dynamically-in-directive-link-function/22117648 and as you can see at the end they've cheated :) be creative :) using compile it's very cpu consuming (try to see how many loops inside the compile function just a lot !) with a lot of headache ^^ – Whisher Mar 02 '14 at 10:56
  • 2
    "I can't add the attributes in a template because it must be attributes to the actual element"… Simply use `replace : true` ? – Blackhole Mar 02 '14 at 13:18
  • @Blackhole Holy F why is this not in the docs!? :O – Secret Mar 02 '14 at 13:57
  • Actually, it's [here in the doc](http://docs.angularjs.org/api/ng/service/$compile#-templateurl-) ;) . – Blackhole Mar 02 '14 at 15:07

0 Answers0