问题描述
@Directive({ selector: ’[myHighlight]’ })@Directive 装饰器需要一个 css 选择器,以便从模板中识别出关联到这个指令的 HTML
这是从官网上面例子copy下来的,有一个疑问:@Directive 装饰器是不是要从所有模板上面找出myHighlight这个选择器?如果是这样的话,不是很耗资源吗?一般情况下不是应该指定某个模板里面找?
问题解答
回答1:文档上写的很清楚:
@Directive requires a CSS selector to identify the HTML in thetemplate that is associated with our directive. The CSS selector foran attribute is the attribute name in square brackets. Our directive’sselector is [myHighlight]. Angular will locate all elements in thetemplate that have an attribute named myHighlight.
应该是在当前模板找所有符合这个选择器的元素,很耗资源是你感觉的。其实和jquery的选择器没什么两样啊。

