Bug
The component should return a change event like any regular HTML input element does, too. Interestingly, MdInput does return a regular change event.
When binding an event handler via the (change)="doSomethingOnChange($event)" notation, the MdCheckbox component does not return a DOM change event when checked/unchecked but a custom angular event, which does not bubble like a change event should.
Simply register a change event handler on an <md-checkbox> and log the event.
http://plnkr.co/edit/4KRXLDmbHU2pO7RvBiaw?p=preview
The event should bubble just like a regular HTML checkbox's events bubble so that container elements can catch their events.
I tried both 2.0.2 and 2.3.0
A fix would be fantastic. We all appreciate your team's great work!
We should make sure we're doing consistent change events across all components.
@jelbourn at least on components that wrap native ones (e.g. checkbox and radio) we could replace the stopPropagation calls with attaching our custom event data to the object. E.g. this seems to work:
(event as any).mdChangeEvent = this._createChangeEvent();
I'm not sure how we'd handle it on elements that don't have a native equivalent, though.
I would rather just let the change event from the underlying input elements bubble up.
Everything inside of our custom events should be still accessible from the native DOM event.
We added the custom event objects because previously those emitted just booleans. See Pull Request https://github.com/angular/material2/pull/554
I agree that the native DOM event is better, but it might not be consistent across all components. E.g. the checkbox one bubbles because it's native, however something like select won't bubble since it doesn't have a native element beneath.
Need to have a boarder conversation about this behavior.
are there any news about this issue?
or is there a possible workaround for it to call stopPropagation otherwise?
I have the same issue
In View HTML -
<span *ngFor="let dataValue of detailData" >
<md-checkbox class="toggle-vis" [attr.data-column]="dataValue" [checked]="true">{{dataValue}}</md-checkbox>
</span>
In Component -
$('md-checkbox.toggle-vis').on( 'click', function (e) {
console.log('here');
});
Above is working fine but
$('md-checkbox.toggle-vis').on( 'change', function (e) {
console.log(this.checked);
});
is not working.
One bad solution is -
$('.mat-checkbox-input').change(function (e) {
console.log('here - ' + this.checked);
});
Here is nice solution via jquery -
`$('md-checkbox.toggle-vis').on( 'click', function (e) {
console.log($(this).find('.mat-checkbox-input').is(":checked"));
});`
just install jquery with npm and put below line in component -
`import * as jQuery from 'jquery';
any news, we would really need to handle stopPropagation
I used both click and change event on mat-checkbox, click for passing the event to stop propagation.
The below code worked for me.
Most helpful comment
I used both click and change event on mat-checkbox, click for passing the event to stop propagation.
The below code worked for me.