A directive which can be element, attribute, css class and comment?

蹲街弑〆低调 提交于 2019-12-19 09:48:07

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/34030046/a-directive-which-can-be-element-attribute-css-class-and-comment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!