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

Commit cba5fa7

Browse files
committed
fix(select): allow circular references
closes #5330, references #3268
1 parent bb83931 commit cba5fa7

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

src/components/select/select.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -478,11 +478,11 @@ function SelectMenuDirective($parse, $mdUtil, $mdTheming) {
478478
// and values matching every option's controller.
479479
self.options = {};
480480

481-
$scope.$watch(function() {
481+
$scope.$watchCollection(function() {
482482
return self.options;
483483
}, function() {
484484
self.ngModel.$render();
485-
}, true);
485+
});
486486

487487
var deregisterCollectionWatch;
488488
var defaultIsEmpty;
@@ -629,6 +629,7 @@ function SelectMenuDirective($parse, $mdUtil, $mdTheming) {
629629
}
630630
}
631631
self.ngModel.$setViewValue(self.isMultiple ? values : values[0]);
632+
self.ngModel.$render();
632633
};
633634

634635
function renderMultiple() {

src/components/select/select.spec.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,13 @@ describe('<md-select>', function() {
221221
expect(selectedOptions(el).length).toBe(0);
222222
});
223223

224+
it('supports circular references', function() {
225+
var opts = [{ id: 1 }, { id: 2 }];
226+
opts[0].refs = opts[1];
227+
opts[1].refs = opts[0];
228+
setup('ng-model="$root.model"', opts, { renderValueAs: 'value.id' });
229+
});
230+
224231
it('renders model change by selecting new and deselecting old', inject(function($rootScope) {
225232
$rootScope.$apply('model = "b"');
226233
var el = setup('ng-model="$root.model"', ['a','b','c']);
@@ -767,10 +774,10 @@ describe('<md-select>', function() {
767774
return el;
768775
}
769776

770-
function setup(attrs, options) {
777+
function setup(attrs, options, compileOpts) {
771778
var el;
772779
inject(function($compile, $rootScope) {
773-
var optionsTpl = optTemplate(options);
780+
var optionsTpl = optTemplate(options, compileOpts);
774781
var fullTpl = '<md-select-menu ' + (attrs || '') + '>' + optionsTpl +
775782
'</md-select-menu>';
776783
el = $compile(fullTpl)($rootScope);
@@ -786,12 +793,13 @@ describe('<md-select>', function() {
786793
return setup(attrs, options);
787794
}
788795

789-
function optTemplate(options) {
796+
function optTemplate(options, compileOpts) {
790797
var optionsTpl = '';
791798
inject(function($rootScope) {
792799
if (angular.isArray(options)) {
793800
$rootScope.$$values = options;
794-
optionsTpl = '<md-option ng-repeat="value in $$values" ng-value="value">{{value}}</md-option>';
801+
var renderValueAs = compileOpts ? compileOpts.renderValueAs || 'value' : 'value';
802+
optionsTpl = '<md-option ng-repeat="value in $$values" ng-value="value">{{' + renderValueAs + '}}</md-option>';
795803
} else if (angular.isString(options)) {
796804
optionsTpl = options;
797805
}

0 commit comments

Comments
 (0)