admin管理员组文章数量:1133921
I'm working in a small team, building in AngularJS and trying to maintain some basic standards & best practices; especially given we're relatively new with Angular.
My question is with regards to Directives. More accurately, the restrict
options.
Some of us are using restrict: 'E'
thus having <my-directive></my-directive>
in the html.
Others are using restrict: 'A'
and having <div my-directive></div>
in the html.
Then, of course, you can use restrict: 'EA'
and use either of the above.
At the moment it's no big deal, though when this project is as big as it's going to get I would like anybody looking at it to easily understand what's going on.
Are there pros/cons to either the attribute or element way of doing things?
Are there any pitfalls we should know, if choosing say element over attribute?
I'm working in a small team, building in AngularJS and trying to maintain some basic standards & best practices; especially given we're relatively new with Angular.
My question is with regards to Directives. More accurately, the restrict
options.
Some of us are using restrict: 'E'
thus having <my-directive></my-directive>
in the html.
Others are using restrict: 'A'
and having <div my-directive></div>
in the html.
Then, of course, you can use restrict: 'EA'
and use either of the above.
At the moment it's no big deal, though when this project is as big as it's going to get I would like anybody looking at it to easily understand what's going on.
Are there pros/cons to either the attribute or element way of doing things?
Are there any pitfalls we should know, if choosing say element over attribute?
Share Improve this question edited Apr 22, 2014 at 13:28 JoeG 13.2k1 gold badge39 silver badges64 bronze badges asked Apr 22, 2014 at 13:24 Darren WainwrightDarren Wainwright 30.7k22 gold badges83 silver badges131 bronze badges7 Answers
Reset to default 189restrict is for defining the directive type, and it can be A
(Attribute), C
(Class), E
(Element), and M
(coMment) , let's assume that the name of the directive is Doc
:
Type : Usage
A =
<div Doc></div>
C =
<div class="Doc"></div>
E =
<Doc data="book_data"></Doc>
M =
<!--directive:Doc -->
According to the documentation:
When should I use an attribute versus an element? Use an element when you are creating a component that is in control of the template. The common case for this is when you are creating a Domain-Specific Language for parts of your template. Use an attribute when you are decorating an existing element with new functionality.
Edit following comment on pitfalls for a complete answer:
Assuming you're building an app that should run on Internet Explorer <= 8, whom support has been dropped by AngularJS team from AngularJS 1.3, you have to follow the following instructions in order to make it working: https://docs.angularjs.org/guide/ie
The restrict option is typically set to:
- 'A' - only matches attribute name
- 'E' - only matches element name
- 'C' - only matches class name
- 'M' - only matches comment
Here is the documentation link.
Element is not supported in IE8 out of the box you have to do some work to make IE8 accept custom tags.
One advantage of using an attribute over an element is that you can apply multiple directives to the same DOM node. This is particularly handy for things like form controls where you can highlight, disable, or add labels etc. with additional attributes without having to wrap the element in a bunch of tags.
One of the pitfalls as I know is IE problem with custom elements. As quoted from the docs:
3) you do not use custom element tags such as (use the attribute version instead)
4) if you do use custom element tags, then you must take these steps to make IE 8 and below happy
<!doctype html>
<html xmlns:ng="http://angularjs.org" id="ng-app" ng-app="optionalModuleName">
<head>
<!--[if lte IE 8]>
<script>
document.createElement('ng-include');
document.createElement('ng-pluralize');
document.createElement('ng-view');
// Optionally these for CSS
document.createElement('ng:include');
document.createElement('ng:pluralize');
document.createElement('ng:view');
</script>
<![endif]-->
</head>
<body>
...
</body>
</html>
Pitfall:
- Using your own html element like
<my-directive></my-directive>
wont work on IE8 without workaround (https://docs.angularjs.org/guide/ie) - Using your own html elements will make html validation fail.
- Directives with equal one parameter can done like this:
<div data-my-directive="ValueOfTheFirstParameter"></div>
Instead of this:
<my-directive my-param="ValueOfTheFirstParameter"></my-directive>
We dont use custom html elements, because if this 2 facts.
Every directive by third party framework can be written in two ways:
<my-directive></my-directive>
or
<div data-my-directive></div>
does the same.
2 problems with elements:
- Bad support with old browsers.
- SEO - Google's engine doesn't like them.
Use Attributes.
本文标签: javascriptAngularJS Directive Restrict A vs EStack Overflow
版权声明:本文标题:javascript - AngularJS Directive Restrict A vs E - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736783301a1952717.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论