Reputation: 4306
I wrote an attribute restricted Angular directive (restrict:'a'
) that adds features to textarea
. It makes no sense to apply it to any other type of element.
Adding a if (element.nodeName == 'TEXTAREA') {
is really dirty and unreliable.
I tried to add require: textarea
to it but it does not work and I get this error: Error: No controller: textarea
Question: Is there any cleaner way to properly apply this restriction?
EDIT: Additional constraint following the first answer.
I want to avoid using a template in this directive as I want to be able to use several directive of this type. Here is an example of what I'd like:
<textarea splittable shared mergeable></textarea>
Upvotes: 1
Views: 667
Reputation: 54649
When using your own directive (eg my-textarea
) with restrict: 'E', replace: true
, any additional attributes will get carried over to the root-element of the directive, even other attribute directives. So:
<my-textarea splittable class="foobar"></my-textarea>
could be rendered as:
<textarea splittable="" class="foobar"></textarea>
with splittable
being executed.
demo: http://jsfiddle.net/LMq3M/
So I think using your own directive is realy the cleanest way to handle this.
Upvotes: 2
Reputation: 166
In my opinion there is no need to add such controls as they just tend to add complex code when the real issue is human error.
Just document what the purpose and usage is for the directive.
Upvotes: 1
Reputation: 240
The idea is you give your directive a unique name, like myDirective and you can use it in HTML as such.
<body>
<my-directive></my-directive>
</body>
The directive will replace the tag with the template you provided in the directive controller. Which could be in your case a simple textarea element with added properties.
I recommend watching this video to clearly grasp the concept of directives. http://www.youtube.com/watch?v=xoIHkM4KpHM
Upvotes: 0