Angular directives - element or attribute?

时光总嘲笑我的痴心妄想 提交于 2019-11-28 06:25:47

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.

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.

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.

I usually defer to the John Papa AngularJS style guide when making these types of decisions. He says:

Lean towards implementing as an element when its standalone and as an attribute when it enhances its existing DOM element.

If you want to keep your HTML valid you'd use attributes, e.g. if you have a directive ipwr-modal, you can declare it as <div data-ipwr-modal="you-could-put-some-binding-here"></div>.

When creating directives with a custom layout, however, you'd better use element declaration (if you don't need to have your HTML valid). This is the more obvious way to say: "hey, we have a custom component here".

This blog post explains it with some more ideas

Some points to consider:

  1. Most of the time attributes is the best/most convenient option (it's not the default by chance).

  2. Anything you can do with element-bound directives, you can do with attribute-bound as well.

  3. Element-bound directives can be more descriptive/readable at times.

  4. If you want your code to pass certain types of validation, you should use attributes.

  5. Since you want to support IE8, keep in mind that custom tags have an extra overhead (more info), which hurts maintainability.


BTW, you can't limit directives to elements only (without affecting functionality), so it is more a question of allowing 'element' or not. Note that an element often has more than one directives and directives placed on the same element can cooperate and augment each other's behaviour. So limiting the use of directives to 'element', means limiting the number of custom directives per element to 1, which severly reduces the functionality-potential.


That said, this is what I ('d) do:

  • If IE8 is not an issue, allow both (mostly use attributes).

  • If IE8 (or the overhead for custom tags) is an issue, use only attributes.

  • In general, if only one form should be allowed, it should be attributes (works anywhere, no extra overhead, offers all functionality).

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