简体   繁体   English

如何在angular6中应用样式

[英]How to apply style in angular6

I want to apply text color when expected id will clicked 我希望点击预期ID时应用文本颜色

     <nav class="navbar ">
        <ul class="navbar-nav">
          <li *ngFor="let item of items">
         <a class="nav-link" >{{item.title}}</a>
          </li>
        </ul>
      </nav>


items=[{id:1,title:"a"},{id:2,title:"b"},{id:2,title:"c"},{id:3,title:"d"}]

controller- 控制器 -

private name; 私人名称;

this.routes.params.subscribe((params)=>{
      this.data=params['id'];
      console.log(this.data);
    })  

this.Service.getAllItems().subscribe((data)=>{
   this.items=data;
   for(let i of this.items){
   if(i.id == this.data){
   this.name=i.title;
}
}

For clicked id I have to apply text color red.How to apply it.Please help 对于点击的ID,我必须将文本颜色设置为红色。如何应用。请帮助

You could have a variable with the active id, which is set when you click: 您可能有一个带有活动ID的变量,该变量在单击时设置:

import { Component } from '@angular/core';

    @Component({
      selector: 'my-app',
      templateUrl: './app.component.html',
      styleUrls: [ './app.component.css' ]
    })
    export class AppComponent  {
     public activeId;

      setIdActive(id) {
        this.activeId = id;
      }

    }

And in your html: 并在您的html中:

<nav class="navbar ">
        <ul class="navbar-nav">
          <li *ngFor="let item of items">
         <a (click)="setIdActive(item.id)" class="nav-link" [ngClass]="{'apply-color': item.id == activeId}" >{{item.title}}</a>
          </li>
        </ul>
</nav>

apply-color it's a class with the color you want apply-color这是您想要的颜色的类

This answer caters to condition where we need to highlight all the options which are clicked 此答案适合于需要突出显示所有单击的选项的情况

If you want to change the color of all the links which are clicked and not just the last one, I advise you to use a HostListener directive. 如果要更改所有单击的链接的颜色,而不仅仅是最后一个,请建议使用HostListener指令。

consructor(privte elem: ElementRef, private renderer: Renderer) { }

@HostListener('click',['$event'])  //in your case, the event parameter can be omitted
Click(event: Event) {
    this.renderer.setElementStyle(elem.nativeElement, 'color', 'red');
}

And if just the CSS style for a:visited works for you (not tried by myself), it would be the best solution 而且,如果a:visited的CSS样式适合您(而不是自己尝试),那将是最好的解决方案

You can you [ngClass] for this problem. 您可以为此问题使用[ngClass]。 When you click the link pass the item to the function. 单击链接时,将项目传递给功能。 this will change the activeId. 这将更改activeId。 By using [ngClass] the class will apply to the link. 通过使用[ngClass],该类将应用于链接。

 let activeId:number; makeALinkActive(item){ this.activeId = item.id; } items=[{id:1,title:"a"},{id:2,title:"b"},{id:2,title:"c"},{id:3,title:"d"}] 
 .red{ color: red; } 
 <nav class="navbar "> <ul class="navbar-nav"> <li *ngFor="let item of items"> <a class="nav-link" [ngClass]="{'red':activeId === item.id}" (click)='makeALinkActive(item)'>{{item.title}}</a> </li> </ul> </nav> 

 items=[{id:1,title:"a"},{id:2,title:"b"},{id:2,title:"c"},{id:3,title:"d"}] 

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

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