Criando uma diretiva:输入e属性绑定

markdown
阅读 37 收藏 0 点赞 0 评论 0

Criando uma diretiva: Input e Property Binding
# Criando uma diretiva: Input e Property Binding

```HTML
<p highlight [defaultColor]="'grey'" [highlightColor]="'red'">
    Texto com highlight com cores customizadas.
</p>
```

```typescript
@import { Directive, HostListener, HostBinding, Input } from '@angular/core';

@Directive({
    selector: '[highlight]'
})
export class HighlightDirective {

    @HostListener('mouseenter') onMouseOver(){
        this.backgroundColor = this.highlightColor;
    }

    @HostListener('mouseleave') onMouseLeave(){
        this.backgroundColor = this.defaultColor;
    }

    @HostBinding('style.backgroundColor') background: string;

    // Variáveis para cores dinâmicas
    @Input() defaultColor: string = 'white';
    @Input() highlightColor: string = 'yellow';

    constructor() {}

}
```
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号