Search code examples
angularformscheckboxngmodel

Unable to get the checkbox selected or not in template driven form (angular 7)


  <input type="checkbox" [name]="filter.filterName" ngModel />

let filterValue: string = testForm.form.value[filter.filterName];

filterValue is always returning empty string checkbox when checked and unchecked

how can i know that checkbox is checked or not in template driven form and no data binding. I need value from testForm:NgForm object ?


Solution

  • how can i know that checkbox is checked or not in template driven form and no data binding.

    You can use template variable.

    html:

    <input type="checkbox" #check />
    

    ts:

    @ViewChild('check')
    check: ElementRef;
    

    stackblitz : https://stackblitz.com/edit/angular-udaej4