get the value of the checked box and pass it in a function in button angular
- User
get the value of the checked box and pass it in a function in button angular
- Assistant
To get the value of a checked box and pass it to a function in Angular, you can use two-way data binding and the
(click)event handler.First, define a variable in your component's TypeScript file to hold the value of the checkbox:
selectedValue: string;Then, in your Angular template (HTML file), use the
[(ngModel)]directive to bind the checkbox value to theselectedValuevariable and the(click)event handler to call a function with the selected value when the button is clicked:<input type="checkbox" [(ngModel)]="selectedValue" value="someValue"> Checkbox <button (click)="myFunction(selectedValue)">Submit</button>In the above example, replace
someValuewith the value you want to assign to the checkbox. When the checkbox is checked/unchecked, theselectedValuevariable will be updated automatically.Finally, in your component's TypeScript file, define the function that will be called when the button is clicked:
myFunction(value: string) { console.log(value); // or perform any other action with the value }When the button is clicked, the
myFunctionfunction will be called with the selected value as the parameter.