选择器在角度2中到底做什么? [英] What exactly does a selector do in angular 2?

查看:93
本文介绍了选择器在角度2中到底做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这种情况下选择器会做什么?

What does the selector do in this case?

import { Component } from '@angular/core';
import { HighlightDirective } from './highlight.directive';

@Component({
  selector: 'my-app',
  templateUrl: 'app/app.component.html',
  directives: [HighlightDirective]
})
export class AppComponent { }

在这种情况下它会做什么?

And what does it do in this case?

@Directive({
  selector: '[myHighlight]',
  host: {
    '(mouseenter)': 'onMouseEnter()',
    '(mouseleave)': 'onMouseLeave()'
  }
})

推荐答案

该组件已应用于index.html中的<my-app></my-app>标记.如果您的index.html没有该标签,Angular将在启动时失败.您可以控制Angular应用程序的播放位置.

The component is applied to the <my-app></my-app> tag in your index.html. If your index.html doesn't have that tag Angular will fail at startup. You can control where you Angular application will be played.

这对于使用bootstrap(AppComponent)

指令选择器[myHighlight]将为所有具有myHighlight属性(如<xxx myHighlight>)且其中MyHighLight在指令(如

The directive selector [myHighlight] will create aMyHighlight directive instance for all elements that have a myHighlight attribute like <xxx myHighlight> and where MyHighLight is listed in directives like

@Component({
  selector: '...', 
  directives: [MyHighlight], ...
})
export class Xxx

就像其他组件的指令选择器(通常不是根于AppComponent的根组件)一样,它的工作方式与指令的选择器相同.该组件必须在directives数组中列出.然后,所有与选择器匹配的标签都将升级为Angular组件.

Like the directive selector for other components (that are not the root component like AppComponent usually is), it works the same like the selector for the directive. The component has to be listed in the directives array. Then all tags that match the selector are upgraded to Angular components.

选择器类似于CSS选择器.它们可以是属性选择器,标签选择器,类选择器,id选择器以及它们的组合.还支持:not(...).

Selectors are like CSS selectors. They can be attribute selectors, tag selectors, class selectors, id selectors and combinations of these. Also :not(...) is supported.

不支持选择器,这些选择器需要与父项和子项进行匹配,例如与a ba > ba + b之类的组合器匹配,其中b是同级组件,子组件,取消扫描的对象…….指令或组件选择器始终只能引用单个元素.

What is not supported are selectors that need to match parent and child like with combinators like a b or a > b or a + b where b is a sibling, child, descandant, ... of another component. A directive or component selector can always only refer to a single element.

这篇关于选择器在角度2中到底做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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