【问题标题】:change colour of a mat button when clicked in angular单击角度时更改垫子按钮的颜色
【发布时间】:2020-01-21 08:27:39
【问题描述】:

您好,我正在开发一个 Angular 应用程序。我想创建选项卡,单击每个选项卡都会调用不同的功能。我正在使用垫子按钮。我的代码


<div flexLayout="row">
    <button mat-flat-button [color]="this.paletteColour" (click)="change()">B2-L1</button>
    <button mat-flat-button [color]="this.paletteColour" (click)="change()">B2-L3</button>
    <button mat-flat-button [color]="this.paletteColour" (click)="change()">B2-L3</button>
    <button mat-flat-button [color]="this.paletteColour" (click)="change()">B2-L4</button>

</div>

.ts 文件

 paletteColour
  change() {
  this.paletteColour = 'warn';
  }

.css 文件

.mat-flat-button {
    background-color: grey;
    border-radius: 0 px !important;;  
}

.mat-button, .mat-flat-button, .mat-icon-button, .mat-stroked-button { border-radius: 0px; }

但是当点击一个按钮时,这会改变所有按钮的颜色。如何仅更改一个已单击按钮的颜色,而其余按钮均保持相同颜色。

PS : 只有当我在 css 中评论背景颜色时,颜色变化才有效。

【问题讨论】:

  • 发生这种情况是因为所有按钮都指向同一个变量。您需要创建一个单独的变量或对应于每个按钮的变量数组。
  • 我更多地考虑像活动这样的行,如果单击该按钮仅该按钮颜色更改,则其他按钮必须保持不变
  • 你尝试过原生的javascript方式吗?
  • 另外,你说你调用了多个不同的函数,但实际上你总是调用同一个函数。
  • 嗨,我想进行两个函数调用,一个是更改按钮颜色,另一个是 api 调用,我没有添加其他函数,因为我的主要目标是了解如何更改所选按钮的颜色

标签: javascript html css angular angular-material


【解决方案1】:

由于您提到您专门使用导航,因此我为您提供了另一种解决方案;当路由处于活动状态时,您可以让 Angular 添加一个名为 active 的类。这是由routerLinkActive="active" 实现的。

Stackblitz:https://stackblitz.com/edit/angular-ijn9bz

app.module.ts

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { RouterModule } from '@angular/router'

import { AppComponent } from './app.component';
import { HelloComponent } from './hello.component';
import { HomeComponent } from './home/home.component';
import { Page2Component } from './page2/page2.component';
import { Page3Component } from './page3/page3.component';

@NgModule({
  imports:      [ 
    BrowserModule, 
    FormsModule,
    RouterModule.forRoot([
      { path: 'home', component: HomeComponent },
      { path: 'page2', component: Page2Component },
      { path: 'page3', component: Page3Component }
    ])
  ],
  declarations: [ AppComponent, HelloComponent, HomeComponent, Page2Component, Page3Component ],
  bootstrap:    [ AppComponent ]
})
export class AppModule { }

app.component.html

<div flexLayout="row">
    <button [routerLink]="['/home']" routerLinkActive="active">B2-L1</button>
    <button [routerLink]="['/page2']" routerLinkActive="active">B2-L3</button>
    <button [routerLink]="['/page3']" routerLinkActive="active">B2-L3</button>
</div>

<router-outlet></router-outlet>

app.component.css

.active{
  background: yellow;
}

【讨论】:

  • 如果您不使用导航而只想在按钮之间切换并更改其背景颜色怎么办?
  • @fairlyMinty 您可以将按钮封装在组件中并在那里跟踪其状态。 (请参阅我对这个问题的其他答案)
【解决方案2】:

我想出了一个办法

.html

<div flexLayout="row">
    <div flexLayout="row">
        <button mat-flat-button [ngClass]="this.selected == 1 ? 'tab_selected' : 'tab_unselected'" (click)="change(1)">B2-L1</button>
        <button mat-flat-button [ngClass]="this.selected == 2 ? 'tab_selected' : 'tab_unselected'" (click)="change(2)">B2-L3</button>
        <button mat-flat-button [ngClass]="this.selected == 3 ? 'tab_selected' : 'tab_unselected'" (click)="change(3)">B2-L3</button>
        <button mat-flat-button [ngClass]="this.selected == 4 ? 'tab_selected' : 'tab_unselected'" (click)="change(4)">B2-L4</button>
    </div>
</div>

.ts

selected
change(n) {
  this.paletteColour = 'warn';
  this.selected=n;
  console.log(this.selected);
}

.css

   .tab_selected {
    background-color: grey; 
    border-radius: 0 px !important;;  
}

 .mat-button {
  border-radius: 0 px !important;;     
} 

感谢大家的支持。

【讨论】:

    【解决方案3】:

    你不能像上面那样在angular中应用类(mat的颜色属性)。它应该像下面那样使用

     <div flexLayout="row">
              <button mat-flat-button [attr.color]="this.selected == 1 ? 'warn' : 'primary'" (click)="change(1)">B2-L1</button>
              <button mat-flat-button [attr.color]="this.selected == 2 ? 'warn' : 'primary'" (click)="change(2)">B2-L3</button>
              <button mat-flat-button [attr.color]="this.selected == 3 ? 'warn' : 'primary'" (click)="change(3)">B2-L3</button>
              <button mat-flat-button [attr.color]="this.selected == 4 ? 'warn' : 'primary'" (click)="change(4)">B2-L4</button>
          </div>
    
    change(n) {
        this.paletteColour = 'warn';
        this.selected=n;
      }
    

    【讨论】:

    • 检查 html 并查看类名是否发生变化?
    • 是的类名正在改变,但颜色没有改变
    • 您是否在项目中链接了材质样式表。材质按钮是否在您项目的其他组件中起作用?
    • 材质按钮工作,但颜色没有改变:(
    【解决方案4】:

    您可以将按钮放在它们自己的组件中。我用一个简单的按钮做了一个快速演示。我确信它适用于有棱角的材料。

    一个主要优点是这也分离了功能,而不是创建一个可以做所有事情的组件类。

    import { Component } from '@angular/core';
    
    @Component({
      selector: 'my-button',
      template: '<button [style.background]="this.color" (click)="toggleColor()">I am button #1</button>'
    })
    export class MyButtonComponent  {
      color = 'red';
    
      constructor(){}
    
      toggleColor(){
        if( this.color === 'red' )
          this.color = 'blue';
        else
          this.color = 'red';
      }
    }
    

    玩弄它:https://stackblitz.com/edit/angular-y8zf5a

    注意:我个人不会根据按钮单击为按钮着色,而是更改按钮类以指示按钮调用的功能的可用性。因此,您将拥有反映按钮状态的类,如不可用、正在处理、可用、已处理等。这些类将使用 css 设置样式。

    【讨论】:

    • 嗨,点击时按钮的颜色会从红色变为蓝色,但是当我点击另一个按钮时,蓝色仍然存在,我只希望按钮为蓝色
    • @cvg 我一定误解了这个问题。我假设您想要一排可以独立交互的按钮。你想达到什么目的?按钮导航按钮是否与特定路径相关?
    • 嗨@RMo,是的,它们是特定路径的导航按钮
    【解决方案5】:

    根据您的要求,首先您需要知道哪个选项卡处于活动状态,然后仅为该选项卡应用颜色,为此您需要有另一个变量 currentActiveTab。 每当用户使用 change 方法单击不同的选项卡时,您都需要更改 currentActiveTab 值,这将通过将选项卡索引作为参数传递给该方法来激活选项卡的颜色。

    <div flexLayout="row">
        <button mat-flat-button [color]="this.crrentActiveTab == 1 ? this.paletteColour : null" (click)="change(1)">B2-L1</button>
        <button mat-flat-button [color]="this.crrentActiveTab == 2 ? this.paletteColour : null" (click)="change(2)">B2-L3</button>
        <button mat-flat-button [color]="this.crrentActiveTab == 3 ? this.paletteColour : null" (click)="change(3)">B2-L3</button>
        <button mat-flat-button [color]="this.crrentActiveTab == 4 ? this.paletteColour : null" (click)="change(4)">B2-L4</button>
    </div>
    

    在这里您将在 ts change 方法中设置 palete

     change(index) {
      this.currentActiveTab = index;
     }
    

    【讨论】:

    • 您好,谢谢。我在更改函数中添加了`this.paletteColour = 'warn';`。但我无法改变颜色。
    【解决方案6】:

    使用deep

     .mat-flat-button  /deep/ .mat-button-focus-overlay {
              background-color: grey;
              border-radius: 0 px !important;; 
        }
    

    这将覆盖默认样式

    【讨论】:

    • 不工作 :( ,实际上灰色背景现在也没有被应用
    • a) /deep/ 不再使用;更喜欢 ::ng-deep b) 甚至 ::ng-deep 已被弃用,谷歌浏览器最终会放弃支持。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-03
    • 2019-03-28
    • 2021-12-12
    • 1970-01-01
    • 1970-01-01
    • 2021-07-07
    • 2012-12-06
    相关资源
    最近更新 更多