简体   繁体   English

角度指令中的css选择器

[英]css selector in angular directive

Looking at the documentation for angular directives they list valid selectors.查看角度指令的文档,他们列出了有效的选择器。 https://angular.io/api/core/Directive https://angular.io/api/core/Directive

  1. element-name: Select by element name element-name:按元素名称选择
  2. .class: Select by class name. .class:按类名选择。
  3. [attribute]: Select by attribute name. [属性]:按属性名称选择。
  4. [attribute=value]: Select by attribute name and value. [attribute=value]:按属性名称和值选择。
  5. :not(sub_selector): Select only if the element does not match the sub_selector. :not(sub_selector): 仅当元素与 sub_selector 不匹配时才选择。
  6. selector1, selector2: Select if either selector1 or selector2 matches. selector1, selector2:选择 selector1 或 selector2 是否匹配。

But the only selectors that seems to work in a directive for me is 3 and 6.但是对我来说似乎可以在指令中工作的唯一选择器是 3 和 6。

Here is a stackblitz where I try to select .test css class in a directive and it doesn't work.这是一个堆栈闪电战,我尝试在指令中选择.test css 类,但它不起作用。 https://stackblitz.com/edit/angular-css-class-selector-in-directive https://stackblitz.com/edit/angular-css-class-selector-in-directive

What am I missing?我错过了什么?

Please have a look at the working example.请看一下工作示例。 https://stackblitz.com/edit/angular-css-class-selector-in-directive-xqwriz https://stackblitz.com/edit/angular-css-class-selector-in-directive-xqwriz

you need to register your directive into declaration in app.module .您需要将您的指令注册到app.module声明中。

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';

import { AppComponent, ComponentWithCssSelector, CssClassDirective } from './app.component';

@NgModule({
  imports:      [ BrowserModule, FormsModule ],
  declarations: [ AppComponent, ComponentWithCssSelector, CssClassDirective],
  bootstrap:    [ AppComponent ]
})
export class AppModule { }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM