We know that there are four types of directives:
HTML element (E)
an attribute on an element (A)
a CSS class (C)
comment (M)
We can use restrict: 'EACM'
in a directive. But can someone give a practical example when we need a directive to be element, attribute, css class and comment
? Thanks.
Basically and really quick :
The only real used values are E
and A
.
E
is element directive. This is the logical type of restriction to use for directives injecting content (template
or templateUrl
). This would be the nearest that AngularJS has to offer to webcomponents
.
A
is attribute. This is the logical type of restriction to enhance an element by adding behaviour to an element inside a template or to an E-directive
element (example : ng-click
, ng-show
...) .
C
is more or less useless. Same expected usages as A
directives but less visible when you read your Angular HTML code because you're gonna mix styling css classes and directive css classes. Subject to risk when your designer removes directive classes from your template, for not finding them in your css files (true story). Quicker answer would be: don't use it, use A
instead.
Best off all, M
is plainly totally useless in 99.999% of usages. This is basically here for markup validation (avoiding invalid tags or attributes that can be caused by the E
and A
directives) for psychotics who hope to (and think it's a good/required thing to) have Angular templates validating any w3 validator service. Can also be used for backward compatibility in rare cases. Quicker answer for M
would be: it's an hack, don't use it.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments