Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 7fc18b2

Browse files
mheveryIgorMinar
authored andcommitted
fix(radio): allows data-binding on value property. Closes#316
1 parent fabc9f7 commit 7fc18b2

File tree

3 files changed

+36
-16
lines changed

3 files changed

+36
-16
lines changed

src/jqLite.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ function JQLitePatchJQueryRemove(name, dispatchThis) {
133133
} else {
134134
fireEvent = !fireEvent;
135135
}
136-
for(childIndex = 0, childLength = (children = element.children()).length;
137-
childIndex < childLength;
136+
for(childIndex = 0, childLength = (children = element.children()).length;
137+
childIndex < childLength;
138138
childIndex++) {
139139
list.push(jQuery(children[childIndex]));
140140
}

src/widget/input.js

+16-14
Original file line numberDiff line numberDiff line change
@@ -546,25 +546,24 @@ angularInputType('checkbox', function(inputElement) {
546546
</doc:example>
547547
*/
548548
angularInputType('radio', function(inputElement) {
549-
var widget = this,
550-
value = inputElement.attr('value');
549+
var widget = this;
551550

552551
//correct the name
553552
inputElement.attr('name', widget.$id + '@' + inputElement.attr('name'));
554553
inputElement.bind('click', function() {
555554
widget.$apply(function() {
556555
if (inputElement[0].checked) {
557-
widget.$emit('$viewChange', value);
556+
widget.$emit('$viewChange', widget.$value);
558557
}
559558
});
560559
});
561560

562561
widget.$render = function() {
563-
inputElement[0].checked = value == widget.$viewValue;
562+
inputElement[0].checked = isDefined(widget.$value) && (widget.$value == widget.$viewValue);
564563
};
565564

566565
if (inputElement[0].checked) {
567-
widget.$viewValue = value;
566+
widget.$viewValue = widget.$value;
568567
}
569568
});
570569

@@ -735,15 +734,15 @@ angularWidget('input', function(inputElement){
735734
pattern = new RegExp(pattern.substr(1, pattern.length - 2));
736735
patternMatch = function(value) {
737736
return pattern.test(value);
738-
}
737+
};
739738
} else {
740739
patternMatch = function(value) {
741740
var patternObj = modelScope.$eval(pattern);
742741
if (!patternObj || !patternObj.test) {
743742
throw new Error('Expected ' + pattern + ' to be a RegExp but was ' + patternObj);
744743
}
745744
return patternObj.test(value);
746-
}
745+
};
747746
}
748747
}
749748

@@ -771,6 +770,7 @@ angularWidget('input', function(inputElement){
771770
controller: TypeController,
772771
controllerArgs: [inputElement]});
773772

773+
watchElementProperty(this, widget, 'value', inputElement);
774774
watchElementProperty(this, widget, 'required', inputElement);
775775
watchElementProperty(this, widget, 'readonly', inputElement);
776776
watchElementProperty(this, widget, 'disabled', inputElement);
@@ -864,15 +864,17 @@ angularWidget('textarea', angularWidget('input'));
864864

865865
function watchElementProperty(modelScope, widget, name, element) {
866866
var bindAttr = fromJson(element.attr('ng:bind-attr') || '{}'),
867-
match = /\s*{{(.*)}}\s*/.exec(bindAttr[name]);
868-
widget['$' + name] =
869-
// some browsers return true some '' when required is set without value.
870-
isString(element.prop(name)) || !!element.prop(name) ||
871-
// this is needed for ie9, since it will treat boolean attributes as false
872-
!!element[0].attributes[name];
867+
match = /\s*{{(.*)}}\s*/.exec(bindAttr[name]),
868+
isBoolean = BOOLEAN_ATTR[name];
869+
widget['$' + name] = isBoolean
870+
? ( // some browsers return true some '' when required is set without value.
871+
isString(element.prop(name)) || !!element.prop(name) ||
872+
// this is needed for ie9, since it will treat boolean attributes as false
873+
!!element[0].attributes[name])
874+
: element.attr(name);
873875
if (bindAttr[name] && match) {
874876
modelScope.$watch(match[1], function(scope, value){
875-
widget['$' + name] = !!value;
877+
widget['$' + name] = isBoolean ? !!value : value;
876878
widget.$emit('$validate');
877879
});
878880
}

test/widget/inputSpec.js

+18
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,24 @@ describe('widget: input', function() {
402402
expect(inputs[0].checked).toBe(true);
403403
expect(inputs[1].checked).toBe(false);
404404
});
405+
406+
407+
it('it should work with value attribute that is data-bound', function(){
408+
compile(
409+
'<li>'+
410+
'<input ng:repeat="item in [\'a\', \'b\']" ' +
411+
' type="radio" ng:model="choice" value="{{item}}" name="choice">'+
412+
'</li>');
413+
414+
var inputs = scope.$element.find('input');
415+
expect(inputs[0].checked).toBe(false);
416+
expect(inputs[1].checked).toBe(false);
417+
418+
scope.choice = 'b';
419+
scope.$digest();
420+
expect(inputs[0].checked).toBe(false);
421+
expect(inputs[1].checked).toBe(true);
422+
});
405423
});
406424

407425

0 commit comments

Comments
 (0)