admin管理员组文章数量:1357276
In Angular I have a button which toggles a class - What I want to do is add events based on the button class. Should this be done via an If
statement within a function? So far my code is below:
HTML button
<!-- toggle button -->
<button type="button" class="btn btn-primary mt-3 ml-3 btn-button" (click)="status=!status; func()" [ngClass]="{'btn-danger' : status, 'btn-primary' : !status}" [disabled]="clicked">{{status ? 'Delete' : 'Add'}}</button>
<!-- toggle button -->
Component.ts
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './appponent.html',
styleUrls: ['./appponent.css']
})
export class AppComponent {
public btn: any;
func () {
if (this.btn.hasClass('btn-primary')) {
alert('Primary clicked');
} else if (this.btn.hasClass('btn-danger')) {
alert('Danger clicked');
}
}
}
In Angular I have a button which toggles a class - What I want to do is add events based on the button class. Should this be done via an If
statement within a function? So far my code is below:
HTML button
<!-- toggle button -->
<button type="button" class="btn btn-primary mt-3 ml-3 btn-button" (click)="status=!status; func()" [ngClass]="{'btn-danger' : status, 'btn-primary' : !status}" [disabled]="clicked">{{status ? 'Delete' : 'Add'}}</button>
<!-- toggle button -->
Component.ts
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.ponent.html',
styleUrls: ['./app.ponent.css']
})
export class AppComponent {
public btn: any;
func () {
if (this.btn.hasClass('btn-primary')) {
alert('Primary clicked');
} else if (this.btn.hasClass('btn-danger')) {
alert('Danger clicked');
}
}
}
Share
Improve this question
edited Nov 27, 2019 at 17:56
Adrita Sharma
22.2k10 gold badges75 silver badges85 bronze badges
asked Nov 27, 2019 at 14:03
SoleSole
3,35818 gold badges71 silver badges134 bronze badges
2
- So whats the problem? – Tobias Kaufmann Commented Nov 27, 2019 at 14:11
-
Why don't you just use the
status
flag like you already are? – Pedro Estrada Commented Nov 27, 2019 at 14:12
4 Answers
Reset to default 3You can look this demo may this helps you
You can change class on basics of action
property;
<button (click)="onClick()" [ngClass]="action =='Add' ? 'btn-primary': 'btn-danger'"> {{action}}</button>
On button click
onClick() {
if(this.action == 'Add') {
this.action = 'Delete';
alert('Primary Clicked');
} else {
this.action = 'Add';
alert('Danger clicked');
}
}
Instead of the class name, you can check the status
value.
Try like this:
Working Demo
.html
(click)="func()"
.ts
func() {
this.status = !this.status;
if (this.status) {
alert("Primary clicked");
} else if (!this.status) {
alert("Danger clicked");
}
}
You can use viewchild and ElementRef with a Hostlistener for click event, it would be something like this:
export class AppComponent {
@ViewChild('yourButton') deleteAddButton:ElementRef;
func (class) {
if (deleteAddButton.nativeElement.classList.contains(class)) {
alert('Primary clicked');
} else () {
alert('Danger clicked');
}
}
@HostListener('click', ['$event'])
clickHandler(event) {
if(deleteAddButton.nativeElement.classList.contains('btn-primary')) {
this.func('btn-primary');
}
}
}
In html:
<button #deleteAddButton ...</button>
How about:
<button id="myBtn" type="button" class="btn btn-primary" (click)="func()">{{buttontext}}</button>
TS
buttontext: string = 'ADD';
func () {
let element = document.getElementById("myBtn");
element.classList.toggle("btn-danger");
element.classList.toggle("btn-primary");
this.buttontext = this.buttontext === 'ADD' ? 'DELETE' : 'ADD';
}
本文标签: javascriptHow to change click function based on button class in AngularStack Overflow
版权声明:本文标题:javascript - How to change click function based on button class in Angular - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744074592a2586509.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论