Skip to content
This repository has been archived by the owner on Feb 22, 2018. It is now read-only.

Commit

Permalink
fix(ngModel): ensure checkboxes and radio buttons are flagged as dirt…
Browse files Browse the repository at this point in the history
…y when changed

Closes #569
Closes #585
  • Loading branch information
matsko authored and mhevery committed Feb 19, 2014
1 parent 1f85a8c commit 5766a6a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/directive/ng_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ class InputCheckboxDirective {
};
inputElement.onChange.listen((value) {
scope.$apply(() {
ngModel.dirty = true;
ngModel.viewValue = inputElement.checked
? ngTrueValue.readValue(inputElement)
: ngFalseValue.readValue(inputElement);
Expand Down Expand Up @@ -389,6 +390,7 @@ class InputRadioDirective {
};
radioButtonElement.onClick.listen((_) {
if (radioButtonElement.checked) {
ngModel.dirty = true;
scope.$apply(() => ngModel.viewValue = ngValue.readValue(radioButtonElement));
}
});
Expand Down
52 changes: 52 additions & 0 deletions test/directive/ng_model_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,20 @@ describe('ng-model', () {
expect(element.checked).toBe(false);
}));

it('should render as dirty when checked', inject((Scope scope) {
var element = _.compile('<input type="text" ng-model="my_model" probe="i" />');
Probe probe = _.rootScope.i;
var model = probe.directive(NgModel);

expect(model.pristine).toEqual(true);
expect(model.dirty).toEqual(false);

_.triggerEvent(element, 'change');

expect(model.pristine).toEqual(false);
expect(model.dirty).toEqual(true);
}));


it('should update input value from model using ng-true-value/false', inject((Scope scope) {
var element = _.compile('<input type="checkbox" ng-model="model" ng-true-value="1" ng-false-value="0">');
Expand Down Expand Up @@ -679,6 +693,44 @@ describe('ng-model', () {
expect(greenBtn.checked).toBe(true);
expect(blueBtn.checked).toBe(false);
});

it('should render as dirty when checked', inject((Scope scope) {
var element = _.compile(
'<div>' +
' <input type="radio" id="on" ng-model="my_model" probe="i" value="on" />' +
' <input type="radio" id="off" ng-model="my_model" probe="j" value="off" />' +
'</div>'
);
Probe probe = _.rootScope.i;

var model = probe.directive(NgModel);

var input1 = element.query("#on");
var input2 = element.query("#off");

expect(model.pristine).toEqual(true);
expect(model.dirty).toEqual(false);

expect(input1.classes.contains("ng-dirty")).toBe(false);
expect(input2.classes.contains("ng-dirty")).toBe(false);
expect(input1.classes.contains("ng-pristine")).toBe(true);
expect(input1.classes.contains("ng-pristine")).toBe(true);

input1.checked = true;
_.triggerEvent(input1, 'click');

expect(model.pristine).toEqual(false);
expect(model.dirty).toEqual(true);

input1.checked = false;
input2.checked = true;
_.triggerEvent(input2, 'click');

expect(input1.classes.contains("ng-dirty")).toBe(true);
expect(input2.classes.contains("ng-dirty")).toBe(true);
expect(input1.classes.contains("ng-pristine")).toBe(false);
expect(input1.classes.contains("ng-pristine")).toBe(false);
}));
});

describe('type="search"', () {
Expand Down

0 comments on commit 5766a6a

Please sign in to comment.