Home  >  Q&A  >  body text

angular.js - 关于angular属性指令的疑问

@Directive({ selector: '[myHighlight]' })
@Directive 装饰器需要一个 css 选择器,以便从模板中识别出关联到这个指令的 HTML

这是从官网上面例子copy下来的,有一个疑问:@Directive 装饰器是不是要从所有模板上面找出myHighlight这个选择器?如果是这样的话,不是很耗资源吗?一般情况下不是应该指定某个模板里面找?

曾经蜡笔没有小新曾经蜡笔没有小新2737 days ago572

reply all(1)I'll reply

  • 某草草

    某草草2017-05-15 17:08:18

    The document is very clear:

    @Directive requires a CSS selector to identify the HTML in the
    template that is associated with our directive. The CSS selector for
    an attribute is the attribute name in square brackets. Our directive's
    selector is [myHighlight]. Angular will locate all elements in the
    template that have an attribute named myHighlight.

    It should be to find all the elements that match this selector in the current template, which is very resource-consuming as you think. In fact, it is no different from jquery's selector.

    reply
    0
  • Cancelreply