I have 3 divs and click event, I am using a variable and calling a function in each event.
Now I have an array this.newarray = ["one", "two", "three"];
I want to match the elements from the array with variable. If its success need to show alert
. I have commented statically but I need it from array element. Here is the code below https://stackblitz.com/edit/angular-jk3xsa?file=src%2Fapp%2Fapp.component.ts
app.component.html
<hello name="{{ name }}"></hello>
<p>
Start editing to see some magic happen :)
</p>
<div (click)="click1()">Click1</div>
<div (click)="click2()">Click2</div>
<div (click)="click3()">Click3</div>
app.component.ts
import { Component, onInit } from "@angular/core";
@Component({
selector: "my-app",
templateUrl: "./app.component.html",
styleUrls: ["./app.component.css"]
})
export class AppComponent {
name = "Angular";
newarray: any;
matchoutput: any;
ngOninit() {
this.matchoutput = "one";
this.newarray = ["one", "two", "three"];
}
function1() {
if (this.matchoutput == this.newarray.indexOf(this.matchoutput)) {
alert("hello");
}
/*if (this.matchoutput == "one" || this.matchoutput == "two") {
alert("hello");
}*/
}
click1() {
this.matchoutput = "one";
this.function1();
}
click2() {
this.matchoutput = "two";
this.function1();
}
click3() {
this.matchoutput = "three";
this.function1();
}
}