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

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

问题描述

我所在的团队有大约 6 名 UI 开发人员,他们的质量各不相同,几乎没有 Angular 经验.许多是承包商,对代码库几乎没有经验.该应用程序有一个非常花哨(复杂)的用户界面.它支持 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+).

我们正在引入 Angular 作为应用的主要扩展,我被要求为团队编写 Angular 使用指南.

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 元素,所有元素都以ipwr"为前缀,以避免名称冲突.我试图决定是否强制开发人员给他们的指令限制元素"或属性".只要求一个,以避免混乱和混乱.

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.

我的问题是:对于指令、元素"还是属性",什么限制更好或更受欢迎?我主要关心的是对于刚接触应用程序代码库的几乎没有 Angular 经验的人来说是否易于使用,以减少错误、复制和粘贴行为等.

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.

推荐答案

angular 指南说,当指令完全控制它的模板时,你应该使用元素"限制,这意味着它有一个正在渲染的模板,等

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-click 指令,它被用作属性而不是元素,因为 click 指令只是将点击行为添加到某个元素.

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.

另一个例子是 ng-repeat 指令,它也用作属性而不是元素,因为它将重复使用它的元素.

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.

现在,本指南来自 angular 文档;但是,我不一定知道元素与属性会为您提供更好"的方法,它更像是一种约定.

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.

我个人的偏好是只使用属性限制;主要是因为刚接触 angular 的人一开始看到限制和可以使用的选项的变化时会不知所措.

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.

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

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