角指令 - 元素或属性? [英] Angular directives - element or attribute?

查看:164
本文介绍了角指令 - 元素或属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个团队的一部分,约6 UI的开发者,质量参差不齐以及旁边没有角的经验。许多人承办,与该code碱基的经验。该应用程序有一个很花哨的(复杂)的用户界面。它支持IE8 +(希望很快IE9 +)。

I'm part of a team with about 6 UI devs, of varying quality and next to no Angular experience. Many are contractors, with little experience with the code base. The app has a very fancy (complicated) UI. It supports IE8+ (soon hopefully IE9+).

我们正在引入角为主要扩展到应用程序,我已经被要求写上使用的角度对球队的指导方针。

We're introducing Angular for a major extension to the app, and I've been asked to write guidelines on the use of Angular for the team.

我们将使用指令创建花哨的UI元素,所有的$ P $与IpWr时pfixed,以避免名称冲突。我试图决定是否强制要求开发者给他们的指示限制元素或属性。强制要求只有一个,为了避免混乱和混淆。

We'll use directives to create fancy UI elements, all prefixed with "ipwr" to avoid name clashes. I'm trying to decide whether to mandate that devs give their directives the restriction "element" or "attribute". Mandating only one, to avoid chaos and confusion.

我的问题是:什么是限制更好或更受欢迎的指示,元素或属性?
我最关心的是易用性与小角经验的人谁是新的应用code群,以减少错误,复制和粘贴行为等。

My question is: what restrict is better or more popular for directives, "element" or "attribute"? My main concern is ease of use for people with little Angular experience who are new to the application code base, to reduce bugs, copy and paste behaviour, etc.

推荐答案

角指导说,你应该使用元素的限制,只要该指令已完全控制它的模板,这意味着它有它呈现出一个模板,等等。

The angular guidance says that you should use the "element" restriction whenever the directive has full control over it's template meaning it has a template that it is rendering out, etc.

有关属性,他们建议使用这些只有当你添加的行为,以现有的元素或装饰已有的元素。

For attributes, they suggest to use these only when you are adding "behavior" to an existing element or decorating an existing element.

例如,认为NG-点击指令,这是使用属性而不是一个元素,因为点击指令只是增加了点击行为的一些元素。

For example, think of the ng-click directive, this is used a attribute not as a element because the click directive is just adding the click behavior to some element.

另一个例子是纳克重复指令,它也被用作不作为元件,因为它是要重复在其中它是在所使用的元素的属性

Another example would be the ng-repeat directive, it is also used as an attribute not as a element because it is going to repeat the element in which it is being used in.

现在,这个指导是从角文件;不过,我不知道一定是元素与属性是要给你一个更好的方式它更多的公约。

Now, this guidance is from the angular documentation; however, I don't know necessarily that element vs. attribute is going to give you a "better" approach it's more of a convention.

现在,如果你需要支持旧的浏览器,那么你可能要考虑使用任何注释或类指令。

Now if you have to support older browsers, then you may want to consider using either the comment or class directives.

我个人的preference是只使用属性限制;主要是因为人们认为是新的角获得在第一不知所措当他们看到限制和它是可以使用的选项的变化。

My personal preference is to just use the attribute restriction; mainly because people that are new to angular get overwhelmed at first when they see the restrict and it's variations of the options that can be used.

这篇关于角指令 - 元素或属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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