2

I have an angular 5 material checkbox with a surrounding div and some other elements. I want a click on the div to be the same as a click directly on the checkbox.

Here is my example code:

<div (click)="checked = !checked">
  <mat-checkbox class="example-margin" [(ngModel)]="checked">
                I'm a not working checkbox :'(                  
  </mat-checkbox>
  <span style="background-color: #dddd00;">I want to be clickable too</span>
</div>

and a Stackblitz

At the moment clicking on the checkbox itself is not working, clicking outside the div works. Any ideas?

14
  • how the save workes on Stackblitz ..seems like not saving my code Commented Mar 15, 2018 at 13:47
  • Press the fork button. Commented Mar 15, 2018 at 13:48
  • can you try removing () around ngModel... Commented Mar 15, 2018 at 13:52
  • is that worked ?? Commented Mar 15, 2018 at 13:54
  • 1
    then try this , its working : stackblitz.com/edit/angular-hyvsxv-teyv7t?file=app/… Commented Mar 15, 2018 at 16:30

3 Answers 3

5

Issue here is event of Click on div and [(ngModel)] on checkbox both nullify each other thats the reason its not working.

To make it work you need to stopPropagation, when you click on div element. so i did code as below returning false from div click event that stop it and you code will work.

Working Demo

component.ts

export class CheckboxConfigurableExample {
  checked = false;
  indeterminate = false;
  align = 'start';
  disabled = false;

  onChecked() {
  this.checked= !this.checked;
  return false;
  }
}

component.thml

<mat-card class="result">
    <mat-card-content>
        <h2 class="example-h2">Result</h2>
        <section class="example-section">
            <div (click)="onChecked();">
                <mat-checkbox class="example-margin" [(ngModel)]="checked">
          not working checkbox :'( 
                </mat-checkbox>
        <span  style="background-color: #dddd00;">I want to be clickable too</span>
       </div> 

        </section>
    </mat-card-content>
</mat-card>
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry, but I don't see how this helps with my problem. I know how to use a checkbox ;)
Now you cannot check the checkbox. Only if you click on the "I want to be clickable too"-Part. Stackblitz
3

You can also just do this

<div (click)="checked = !checked">
  <mat-checkbox class="example-margin" [(ngModel)]="checked" (click)="$event.stopPropagation()">
    I'm a not working checkbox :'(
  </mat-checkbox>
  <span style="background-color: #dddd00;">I want to be clickable too</span>
</div>

1 Comment

Thanks. The (click)="$event.stopPropagation()" on the checkbox did it for me.
0

You could do that:

<div (click)="myCheckbox.toggle(); someFunctionWhenChecked(myCheckbox.checked)">
  <mat-checkbox class="example-margin" #myCheckbox (click)="$event.preventDefault()">
                I'm a not working checkbox :'(                  
  </mat-checkbox>
  <span style="background-color: #dddd00;">I want to be clickable too</span>
</div>

explanation:

Set a template variable for the checkbox - #myCheckbox, then prevent default behavior when clicking on checkbox so it wont contradict the action made by the div's click event.

On the div's click event just toggle the checkbox's state, and then if you want something to happen when the state is changed use the "someFunctionWhenChecked()" function to pass the new checkbox state.

Comments

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.