Skip to content

Commit a006d89

Browse files
committed
fix(ionCheckbox): make ng-checked and ng-change work
Closes #1349. Closes #1361 BREAKING CHANGE: ion-checkbox no longer has an isolate scope. This will break your checkbox only if you were relying upon the checkbox having an isolate scope: if you were referencing `$parent.value` as the ng-disabled attribute, for example. Change your code from this: ```html <ion-checkbox ng-disabled="{{$parent.isDisabled}}"></ion-checkbox> ``` To this: ```html <ion-checkbox ng-disabled="{{isDisabled}}"></ion-checkbox> ```
1 parent 07da4ce commit a006d89

File tree

3 files changed

+41
-20
lines changed

3 files changed

+41
-20
lines changed

js/angular/directive/checkbox.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,30 @@ IonicModule
2121
restrict: 'E',
2222
replace: true,
2323
require: '?ngModel',
24-
scope: {
25-
ngModel: '=?',
26-
ngValue: '=?',
27-
ngChecked: '=?',
28-
ngDisabled: '=?',
29-
ngChange: '&'
30-
},
3124
transclude: true,
32-
3325
template: '<label class="item item-checkbox">' +
3426
'<div class="checkbox checkbox-input-hidden disable-pointer-events">' +
35-
'<input type="checkbox" ng-model="ngModel" ng-value="ngValue" ng-change="ngChange()">' +
27+
'<input type="checkbox">' +
3628
'<i class="checkbox-icon"></i>' +
3729
'</div>' +
3830
'<div class="item-content disable-pointer-events" ng-transclude></div>' +
3931
'</label>',
40-
4132
compile: function(element, attr) {
4233
var input = element.find('input');
43-
if(attr.name) input.attr('name', attr.name);
44-
if(attr.ngChecked) input.attr('ng-checked', attr.ngChecked);
45-
if(attr.ngDisabled) input.attr('ng-disabled', attr.ngDisabled);
46-
if(attr.ngTrueValue) input.attr('ng-true-value', attr.ngTrueValue);
47-
if(attr.ngFalseValue) input.attr('ng-false-value', attr.ngFalseValue);
34+
forEach({
35+
'name': attr.name,
36+
'ng-value': attr.ngValue,
37+
'ng-model': attr.ngModel,
38+
'ng-checked': attr.ngChecked,
39+
'ng-disabled': attr.ngDisabled,
40+
'ng-true-value': attr.ngTrueValue,
41+
'ng-false-value': attr.ngFalseValue,
42+
'ng-change': attr.ngChange
43+
}, function(value, name) {
44+
if (isDefined(value)) {
45+
input.attr(name, value);
46+
}
47+
});
4848
}
4949

5050
};

test/html/toggle.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ <h1 class="title">Toggle</h1>
1717
<div ng-controller="TestCtrl">
1818

1919
<div class="list">
20+
<ion-checkbox ng-model="checkModel" ng-change="myChange = checkModel">
21+
Main Check
22+
</ion-checkbox>
23+
<ion-checkbox ng-checked="myChange">
24+
Follows ng-change of Main Check
25+
</ion-checkbox>
2026
<ion-toggle ng-model="myModel" ng-disabled="isDisabled">myModel ({{!!myModel}})</ion-toggle>
2127
<ion-toggle ng-model="catModel" ng-disabled="isDisabled" ng-true-value="cats" ng-false-value="dogs">Cats or dogs? ({{catModel}})</ion-toggle>
2228
<ion-toggle ng-model="isDisabled">Disable myModel ({{!!isDisabled}})</ion-toggle>

test/unit/angular/directive/checkbox.unit.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,28 @@ describe('Ionic Checkbox', function() {
2828
});
2929

3030
it('should pass down attrs', function() {
31-
el = compile('<ion-checkbox ng-checked=1 ng-disabled=2 ng-true-value=3 ng-false-value=4>')(scope);
31+
el = compile('<ion-checkbox name="name" ng-model="model" ng-checked="checked" ng-disabled="disabled" ng-true-value="true-value" ng-false-value="false-value" ng-change="change">')(scope);
3232
scope.$apply();
3333
var input = el.find('input');
34-
expect(input.attr('ng-checked')).toBe('1');
35-
expect(input.attr('ng-disabled')).toBe('2');
36-
expect(input.attr('ng-true-value')).toBe('3');
37-
expect(input.attr('ng-false-value')).toBe('4');
34+
expect(input.attr('name')).toBe('name');
35+
expect(input.attr('ng-model')).toBe('model');
36+
expect(input.attr('ng-checked')).toBe('checked');
37+
expect(input.attr('ng-disabled')).toBe('disabled');
38+
expect(input.attr('ng-true-value')).toBe('true-value');
39+
expect(input.attr('ng-false-value')).toBe('false-value');
40+
expect(input.attr('ng-change')).toBe('change');
41+
});
42+
43+
it('shouhld ngChecked properly', function() {
44+
el = compile('<ion-checkbox ng-checked="shouldCheck">')(scope);
45+
scope.$apply();
46+
var input = el.find('input');
47+
expect(input[0].hasAttribute('checked')).toBe(false);
48+
scope.$apply('shouldCheck = true');
49+
expect(input[0].hasAttribute('checked')).toBe(true);
50+
scope.$apply('shouldCheck = false');
51+
expect(input[0].hasAttribute('checked')).toBe(false);
52+
3853
});
3954

4055
});

0 commit comments

Comments
 (0)