Apply default style and onClick change style of a button -Angular 4

牧云@^-^@ 提交于 2020-12-29 07:36:27

问题


I have one button, i want to apply a default style of a button and when user click on a button change a button style color to red and background-color to white. Blow is my .css and component.

.btn-default {
  color: white;
  background-color: blue;

}

.btn-change {
  color: Red;
  background-color: white;
}

Component.ts

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

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

  bntStyle: string;
  AppComponent() {

   this. bntStyle = 'btn-default';
  }
  submit() {
    this.bntStyle = 'btn-change';

  }

.html

<div>
 <button name="Save" [ngClass]="['bntStyle']" (onClick)="submit()">Submit</button>
</div>

回答1:


You are binding the string 'btnStyle'. Instead, you should bind the filed:

<div>
    <button name="Save" [ngClass]="[bntStyle]" (click)="submit()">Submit</button>
</div>



回答2:


Fire change (OnClick) to (click) and then use following code snippet.

<div>
 <button name="Save" [ngClass]="[bntStyle]" (click)="submit()">Submit</button>
</div>

DEMO



来源:https://stackoverflow.com/questions/48243620/apply-default-style-and-onclick-change-style-of-a-button-angular-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!