Angular中有没有实现下面需求的概念?
创始人
2024-10-31 20:02:05
0

可以使用Angular中提供的Directive来实现,具体实现方法如下:

HTML代码部分:


TypeScript代码部分:

import { Directive, ElementRef, HostListener, Input } from '@angular/core';

@Directive({
  selector: '[appChangeText]'
})

export class ChangeTextDirective {

  @Input() fruitColor: string;

  constructor(private el: ElementRef) {}

  @HostListener('click') onClick() {
    this.el.nativeElement.style.color = this.fruitColor;
  }
}

其中,appChangeText是Directive的selector,ChangeTextDirective是Directive的类名。

在HTML中,我们在

{{selectedFruit}} is my favourite fruit. Click below to change the color.

TypeScript代码

import { Directive, ElementRef, HostListener, Input } from '@angular/core';

@Directive({
  selector: '[appChangeText]'
})

export class ChangeTextDirective {

  @Input() fruitColor: string;

  constructor(private el: ElementRef) {}

  @HostListener('click') onClick() {
    this.el.nativeElement.style.color = this.fruitColor;
  }
}

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})

export class AppComponent {

  fruits = ['Apple', 'Banana

相关内容

热门资讯

透视解密"德州透视h... 透视解密"德州透视hhpoker"其实真的有辅助挂(哔哩哔哩)德州透视hhpoker能透视中分为三种...
透视推荐"wepok... 透视推荐"wepoker永久免费脚本"真是真的是有辅助技巧(哔哩哔哩)1、wepoker永久免费脚本...
透视详情"wepok... 透视详情"wepoker怎么挂飞机"本来真的是有辅助工具(哔哩哔哩)1、首先打开wepoker怎么挂...
透视推荐"wepok... 透视推荐"wepoker黑侠辅助器"原来真的是有辅助挂(哔哩哔哩)1、玩家可以在wepoker黑侠辅...
透视辅助"约局吧是否... 透视辅助"约局吧是否有挂"确实存在有辅助app(哔哩哔哩)1、约局吧是否有挂破解器简单,约局吧是否有...
透视分享"德州局怎么... 透视分享"德州局怎么透视"一直真的是有辅助挂(哔哩哔哩)1.德州局怎么透视 选牌创建新账号,点击进入...
透视总结"wpk模拟... 透视总结"wpk模拟器是什么"本来是真的辅助神器(哔哩哔哩)wpk模拟器是什么能透视中分为三种模型:...
透视了解"wepok... 透视了解"wepoker轻量版辅助"真是存在有辅助脚本(哔哩哔哩)在进入wepoker轻量版辅助软件...
透视详情"wpk模拟... 透视详情"wpk模拟器多开"切实真的是有辅助方法(哔哩哔哩)1、wpk模拟器多开免费脚本咨询教程、w...
透视必备"wepok... 透视必备"wepoker究竟有没有透视"切实是真的辅助方法(哔哩哔哩)1、任何wepoker究竟有没...