小编Sur*_*gar的帖子

Angular 4 Directive选择器仅用于定位组件内的元素

如何在Angular Directive中定位组件内的特定元素.
我有一个带选择器"highlighter"的组件.该组件具有使用ng-content的内容项目.
有一个选择器"荧光笔输入"的指令.
我相信这个指令应该仅应用于突出显示器组件内部的任何输入,但它应用于应用程序中的所有输入 - 这是错误的吗?

Plunker:https://plnkr.co/edit/4zd31C p = preview

@Component({
  selector: 'highlighter',
  template: `
    This should be highlighted: 
    <ng-content></ng-content>
  `
})
export class HighlighterComponent {
}

@Directive({
  selector: 'highlighter input'
})
export class HighlightDirective {
    constructor(el: ElementRef) {
       el.nativeElement.style.backgroundColor = 'yellow';
    }
}
Run Code Online (Sandbox Code Playgroud)

directive selector angular

10
推荐指数
1
解决办法
2765
查看次数

标签 统计

angular ×1

directive ×1

selector ×1