Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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>
like image 310
Shakeer Hussain Avatar asked Jan 13 '18 19:01

Shakeer Hussain


People also ask

How do I change the style of a button in HTML?

Use the :hover selector to change the style of a button when you move the mouse over it.

How do I change the default button style in Android Studio?

This means that there is a drawable called btn_default set as button background. Now we need to find a file named btn_default. * in one of the drawable folders under android-sdk\platforms\android-15\data\res . This can be either an image (very unlikely) or a xml file like btn_default.

How do you change the color of a clicked button in CSS?

To change the background color of the button, use the CSS background-color property and give it a value of a color of your taste. In the . button selector, you use background-color:#0a0a23; to change the background color of the button.


2 Answers

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

<div>
    <button name="Save" [ngClass]="[bntStyle]" (click)="submit()">Submit</button>
</div>
like image 128
Ayala Avatar answered Sep 18 '22 15:09

Ayala


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

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

DEMO

like image 39
santosh singh Avatar answered Sep 17 '22 15:09

santosh singh