一个指令可以是元素,属性,CSS类和注释? [英] A directive which can be element, attribute, css class and comment?

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

问题描述

我们知道有四种类型的指令:

We know that there are four types of directives:

HTML element (E)
an attribute on an element (A)
a CSS class (C)
comment (M)

我们可以在指令中使用restrict: 'EACM'.但是当我们需要将指令设为element, attribute, css class and comment时,有人可以举一个实用示例吗?谢谢.

We can use restrict: 'EACM' in a directive. But can someone give a practical example when we need a directive to be element, attribute, css class and comment? Thanks.

推荐答案

基本且非常快捷:

唯一实际使用的值是 E A .

The only real used values are E and A.

E 是元素指令.这是用于指令注入内容(templatetemplateUrl)的限制的逻辑类型.这将是AngularJS必须提供给webcomponents的最接近的值.

E is element directive. This is the logical type of restriction to use for directives injecting content (template or templateUrl). This would be the nearest that AngularJS has to offer to webcomponents.

A 是属性.通过将行为添加到模板内的元素或E-directive元素(例如:ng-clickng-show ...)上来增强元素的一种逻辑限制类型.

A is attribute. This is the logical type of restriction to enhance an element by adding behaviour to an element inside a template or to an E-directive element (example : ng-click, ng-show...) .

C 几乎没有用.与A指令相同的预期用法,但在阅读Angular HTML代码时不那么可见,因为您将混合样式css类和指令css类.当设计人员从模板中删除指令类时,可能会冒风险,因为它们无法在css文件中找到它们(真实的故事).更快的答案是:不要使用它,而是使用A.

C is more or less useless. Same expected usages as A directives but less visible when you read your Angular HTML code because you're gonna mix styling css classes and directive css classes. Subject to risk when your designer removes directive classes from your template, for not finding them in your css files (true story). Quicker answer would be: don't use it, use A instead.

最棒的是, M 在99.999%的使用中显然完全没有用.基本上是在这里进行标记验证(避免由EA指令引起的无效标记或属性)供那些希望(并认为这是一件好事/必需品)让Angular模板来验证任何w3的精神病患者验证器服务.在极少数情况下,也可以用于向后兼容. M的更快答案是:这是一种hack,请不要使用它.

Best off all, M is plainly totally useless in 99.999% of usages. This is basically here for markup validation (avoiding invalid tags or attributes that can be caused by the E and A directives) for psychotics who hope to (and think it's a good/required thing to) have Angular templates validating any w3 validator service. Can also be used for backward compatibility in rare cases. Quicker answer for M would be: it's an hack, don't use it.

这篇关于一个指令可以是元素,属性,CSS类和注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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