0

I have a button that gets its value and text dynamically from a component. Upon click of the button, I would like to pass the value to a function in the component.

<button class="btn btn-sm btn-primary" 
value="{{ actionButton.value }}"
(click)="doSomething()">{{ actionButton.text }}</button>
doSomething() {

//Get value from the button clicked.
//Do something with it.

}

Is there a way to pass the event?

1
  • 1
    (click)="doSomething(actionButton.value)"? Commented Mar 7, 2019 at 19:13

2 Answers 2

1

You can pass parameters to your methods.

<button class="btn btn-sm btn-primary" 
    value="{{ actionButton.value }}"
    (click)="doSomething(actionButton)">{{ actionButton.text }}</button>
doSomething(actionButton: {value: any}) {
  console.log(actionButton.value);
}
Sign up to request clarification or add additional context in comments.

Comments

0

So...

currentComponent.component.ts has public actionButton: {} ?

currentComponent.component.html has <button (click)="doSomething()"></button> ?

if currentComponent is your component & is not in an *ngFor loop then why would you pass actionButton.value to the DOM to give it back to the component as argument?

You could simply do doSomething(): void { // do something with actionButton.value }

If actionButton is given to you via @Input() the logic remains the same.

3 Comments

The button generation is in a *ngFor loop
My bad. doSomething(value: typeOfYourValue): void {} and in html: doSomething(actionButton.value)
Assuming *ngFor="let actionButton of actionButtons" And.. you can attribute bind over string interpolation with [value]=actionButton.value over value = {{actionButton.value}}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.