AngularJS 指令限制 A 与 E [英] AngularJS Directive Restrict A vs E

查看:19
本文介绍了AngularJS 指令限制 A 与 E的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个小团队中工作,使用 AngularJS 进行构建并尝试维护一些基本标准 &最佳实践;尤其是考虑到我们对 Angular 还比较陌生.

我的问题是关于指令的.更准确地说,是 restrict 选项.

我们中的一些人使用 restrict: 'E' 因此在 html 中有 <my-directive></my-directive>.

其他人正在使用 restrict: 'A' 并在 html 中使用

.

然后,当然,您可以使用 restrict: 'EA' 并使用上述任何一种.

目前这没什么大不了的,但当这个项目达到它的规模时,我希望看到它的任何人都能轻松了解正在发生的事情.

属性或元素的做事方式有优点/缺点吗?

如果选择say element而不是attribute,有什么我们应该知道的陷阱吗?

解决方案

根据文档:

<块引用>

什么时候应该使用属性而不是元素?什么时候使用元素您正在创建一个控制模板的组件.这常见的情况是当您创建特定于域的模板部分的语言.使用属性时用新功能装饰现有元素.

编辑以下关于陷阱的评论以获得完整答案:

假设您正在构建一个应在 Internet Explorer <= 8 上运行的应用程序,AngularJS 团队已从 AngularJS 1.3 中删除了该应用程序,您必须按照以下说明进行操作以使其正常工作:https://docs.angularjs.org/guide/ie

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?

解决方案

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

这篇关于AngularJS 指令限制 A 与 E的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆