简体   繁体   English

如何使用角度更改鼠标输入的文本颜色

[英]how to change color of text on mouse enter using angular

i am a newbie to programming. 我是编程的新手。 I have tried this code but @hostlistner part is not working i have used bootstrap version 4 我已经尝试过此代码,但是@hostlistner部分无法正常工作,我使用了Bootstrap版本4

it doesn't give any compile error also 它也没有给出任何编译错误

 element.nativeElement.style.color = 'red' 

this statement is working but 该声明有效,但

 this.element.nativeElement.style.color = 'blue';

this one doesn't 这个不

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

         @Directive({
          selector: '[setmycolor]'
           })
          export class SetmycolorDirective {

          constructor(private element:ElementRef) {
          element.nativeElement.style.color = 'red';
          }


          @HostListener('onmouseenter')onMouseEnter(){
          this.element.nativeElement.style.color = 'blue';
          }

          }

the code in app 应用程式中的程式码

Try below : 请尝试以下方法:

The name of the listening event is mouseenter , not onmouseenter . 监听事件的名称是的mouseenter,OnMouseEnter在 Hopefully you got it.:) 希望你明白了。:)

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

         @Directive({
          selector: '[setmycolor]'
           })
          export class SetmycolorDirective {

          constructor(private element:ElementRef) {
          element.nativeElement.style.color = 'red';
          }


          @HostListener('mouseenter') onMouseEnter(){ //SEE HERE
          this.element.nativeElement.style.color = 'blue';
          }

          }

Your code is correct, just replace onmouseenter with mouseenter 你的代码是正确的,只是用了mouseenter取代OnMouseEnter在

@HostListener('mouseenter') onMouseEnter(){
          this.element.nativeElement.style.color = 'blue';
          }

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

相关问题 当我在输入标签中以角度6输入一些文本时,如何更改发送按钮颜色? - how to change send button color when I enter some text in my input tag in angular 6? 如何更改 Angular Material 中的主要文本颜色? - How to change primary text color in Angular Material? 如何更改 Angular 主题默认文本颜色? - How to change Angular themes default text color? Angular (TypeScript) - 如何在点击时更改文本颜色 - Angular (TypeScript) - How to change the color of text on click 如何在数组上找到 min 、 max 以及如何使用 angular 更改最小绿色和最大红色的颜色文本 - How to find min , max on array and how to change color text for min green color and max red color using angular 如何在数组上找到最小值、最大值以及如何使用角度更改最小值绿色和最大值红色的颜色文本 - How to find min , max value on array and how to change color text for min value green color and max value red color using angular 如何使用Angular更改背景颜色 - How to change background color using Angular 如何使用角度6中的classname更改背景颜色? - How to change the background color using classname in angular 6? 如何使用Nativescript在菜单中更改颜色文本 - How to change color text in menu, using Nativescript 如何在Angular 6材质中更改文本框占位符颜色 - How to change text box placeholder color in Angular 6 Material
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM