Skip to content
This repository was archived by the owner on Oct 2, 2019. It is now read-only.

Commit f28934b

Browse files
committed
Merge pull request #193 from angular-ui/fix-multiple-setviewvalue
fix(multiple): setViewValue compatible with 1.3-rc.1+
2 parents a9e9f14 + 920b2b6 commit f28934b

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

src/select.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -579,9 +579,9 @@
579579
result;
580580
if ($select.multiple){
581581
var resultMultiple = [];
582-
for (var j = inputValue.length - 1; j >= 0; j--) {
582+
for (var j = $select.selected.length - 1; j >= 0; j--) {
583583
locals = {};
584-
locals[$select.parserResult.itemName] = inputValue[j];
584+
locals[$select.parserResult.itemName] = $select.selected[j];
585585
result = $select.parserResult.modelMapper(scope, locals);
586586
resultMultiple.unshift(result);
587587
}
@@ -730,10 +730,8 @@
730730
});
731731

732732
if ($select.multiple){
733-
scope.$watchCollection('$select.selected', function(newValue) {
734-
//On v1.2.19 the 2nd and 3rd parameteres are ignored
735-
//On v1.3.0-beta+ 3rd parameter (revalidate) is true, to force $parsers to recreate model
736-
ngModel.$setViewValue(newValue, null, true);
733+
scope.$watchCollection('$select.selected', function() {
734+
ngModel.$setViewValue(Date.now()); //Set timestamp as a unique string to force changes
737735
});
738736
focusser.prop('disabled', true); //Focusser isn't needed if multiple
739737
}else{

test/select.spec.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,6 +1337,32 @@ describe('ui-select tests', function() {
13371337

13381338
});
13391339

1340+
it('should change viewvalue only once when updating modelvalue', function () {
1341+
1342+
scope.selection.selectedMultiple = ['[email protected]', '[email protected]'];
1343+
1344+
var el = compileTemplate(
1345+
'<ui-select ng-change="onlyOnce()" multiple ng-model="selection.selectedMultiple" theme="bootstrap" style="width: 800px;"> \
1346+
<ui-select-match placeholder="Pick one...">{{$item.name}} &lt;{{$item.email}}&gt;</ui-select-match> \
1347+
<ui-select-choices repeat="person.email as person in people | filter: $select.search"> \
1348+
<div ng-bind-html="person.name | highlight: $select.search"></div> \
1349+
<div ng-bind-html="person.email | highlight: $select.search"></div> \
1350+
</ui-select-choices> \
1351+
</ui-select> \
1352+
'
1353+
);
1354+
1355+
scope.counter = 0;
1356+
scope.onlyOnce = function(){
1357+
scope.counter++;
1358+
}
1359+
1360+
clickItem(el, 'Nicole');
1361+
1362+
expect(scope.counter).toBe(1);
1363+
1364+
});
1365+
13401366
});
13411367

13421368

0 commit comments

Comments
 (0)