Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

get the value of the checked box and pass it in a function in button angular

  1. User

    get the value of the checked box and pass it in a function in button angular

  2. 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 the selectedValue variable 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 someValue with the value you want to assign to the checkbox. When the checkbox is checked/unchecked, the selectedValue variable 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 myFunction function will be called with the selected value as the parameter.