简体   繁体   English

Angular:Angular 是否支持在同一个 DOM 元素上使用两个自定义指令?

[英]Angular: Does Angular support two custom directives on the same DOM element?

I was following the example on creating a custom directive from angular.io .我正在按照从 angular.io 创建自定义指令的示例进行操作 The original code is hosted on stackblitz .原始代码托管在stackblitz 上 However, when I modified the code to create a secondary directive and applied it to the same element, I do not see it getting applied nor do I see any errors thrown.但是,当我修改代码以创建辅助指令并将其应用于同一元素时,我没有看到它被应用,也没有看到抛出任何错误。

So, my questions are -所以,我的问题是——

  1. Does angular not support two directives on the same element ? angular 不支持同一元素上的两个指令吗? I have found this which says that two structural directives cannot be on one element but not sure about custom directives.我发现表明两个结构指令不能在一个元素上,但不确定自定义指令。
  2. If they are supported then can someone identify why the above code is not working.如果它们受到支持,那么有人可以确定为什么上述代码不起作用。

Thanks.谢谢。

highlight.directive.ts:亮点.directive.ts:

@Directive({
  selector: '[lineUnderline]',
})
export class lineUnderlineDirective {
  constructor(private el: ElementRef) {}

  @Input() defaultColor = '';

  @Input('lineUnderline') underlineColor = '';

  @HostListener('mouseenter') onMouseEnter() {
    this.underline(this.underlineColor || this.defaultColor || 'red');
  }

  @HostListener('mouseleave') onMouseLeave() {
    this.underline('');
  }

  private underline(color: string) {
    this.el.nativeElement.style.textdecoration = 'underline';
    this.el.nativeElement.style.textdecorationcolor = 'blue';
    this.el.nativeElement.style.textdecorationthickness = '3px';
  }
}

app.component.html: app.component.html:

<h1>My First Attribute Directive</h1>

<h2>Pick a highlight color</h2>
<div>
  <input type="radio" name="colors" (click)="color = 'lightgreen'" />Green
  <input type="radio" name="colors" (click)="color = 'yellow'" />Yellow
  <input type="radio" name="colors" (click)="color = 'cyan'" />Cyan
</div>
<p appHighlight lineUnderline>Highlight me!</p>

<p [appHighlight]="color" defaultColor="violet" lineUnderline>
  Highlight me too!
</p>

<hr />
<h2>Mouse over the following lines to see fixed highlights</h2>

<p [appHighlight]="'gray'" lineUnderline>Highlighted in yellow</p>
<p appHighlight="orange" lineUnderline>Highlighted in orange</p>

Well, if the problem is that you dont see underline when you hover it then you are accessing wrong style properties:好吧,如果问题是当您悬停它时没有看到下划线,那么您访问了错误的样式属性:

textdecoration should be => textDecoration textdecoration应该是 => textDecoration

textdecorationcolor should be => textDecorationColor textdecorationcolor应该是 => textDecorationColor

textdecorationthickness should be => textdecorationthickness textdecorationthickness应该是 => textdecorationthickness

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

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