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

Commit 053d671

Browse files
committed
fix(sidenav): allow for data bindings in md-component-id
Fixes the sidenav not evaluating expressions inside of the `md-component-id` attribute. Fixes #9052.
1 parent 9fa6a97 commit 053d671

File tree

2 files changed

+33
-21
lines changed

2 files changed

+33
-21
lines changed

src/components/sidenav/sidenav.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,7 @@ function SidenavDirective($mdMedia, $mdUtil, $mdConstant, $mdTheming, $animate,
249249
},
250250
controller: '$mdSidenavController',
251251
compile: function(element) {
252-
element.addClass('md-closed');
253-
element.attr('tabIndex', '-1');
252+
element.addClass('md-closed').attr('tabIndex', '-1');
254253
return postLink;
255254
}
256255
};
@@ -481,9 +480,8 @@ function SidenavDirective($mdMedia, $mdUtil, $mdConstant, $mdTheming, $animate,
481480
* @ngdoc controller
482481
* @name SidenavController
483482
* @module material.components.sidenav
484-
*
485483
*/
486-
function SidenavController($scope, $element, $attrs, $mdComponentRegistry, $q) {
484+
function SidenavController($scope, $attrs, $mdComponentRegistry, $q, $interpolate) {
487485

488486
var self = this;
489487

@@ -505,5 +503,10 @@ function SidenavController($scope, $element, $attrs, $mdComponentRegistry, $q) {
505503
self.toggle = function() { return self.$toggleOpen( !$scope.isOpen ); };
506504
self.$toggleOpen = function(value) { return $q.when($scope.isOpen = value); };
507505

508-
self.destroy = $mdComponentRegistry.register(self, $attrs.mdComponentId);
506+
// Evaluate the component id.
507+
var rawId = $attrs.mdComponentId;
508+
var componentId = rawId && rawId.indexOf($interpolate.startSymbol()) > -1 ?
509+
$interpolate(rawId)($scope.$parent) : rawId;
510+
511+
self.destroy = $mdComponentRegistry.register(self, componentId);
509512
}

src/components/sidenav/sidenav.spec.js

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -168,13 +168,13 @@ describe('mdSidenav', function() {
168168

169169
describe('controller', function() {
170170
it('should create controller', function() {
171-
var el = setup('');
171+
var el = setup();
172172
var controller = el.controller('mdSidenav');
173173
expect(controller).not.toBe(undefined);
174174
});
175175

176176
it('should open and close and toggle', inject(function($timeout) {
177-
var el = setup('');
177+
var el = setup();
178178
var scope = el.isolateScope();
179179
var controller = el.controller('mdSidenav');
180180

@@ -213,7 +213,7 @@ describe('mdSidenav', function() {
213213
}));
214214

215215
it('should open(), close(), and toggle() with promises', function() {
216-
var el = setup('');
216+
var el = setup();
217217
var scope = el.isolateScope();
218218
var controller = el.controller('mdSidenav');
219219

@@ -257,7 +257,7 @@ describe('mdSidenav', function() {
257257
});
258258

259259
it('should open() to work multiple times before close()', function() {
260-
var el = setup('');
260+
var el = setup();
261261
var controller = el.controller('mdSidenav');
262262

263263
var openDone = 0, closeDone = 0;
@@ -351,23 +351,32 @@ describe('mdSidenav', function() {
351351
});
352352

353353
describe('$mdSidenav lookups', function() {
354-
var $rootScope, $timeout;
354+
var $rootScope, $timeout, $mdSidenav;
355355

356-
beforeEach(inject(function(_$rootScope_, _$timeout_) {
356+
beforeEach(inject(function(_$rootScope_, _$timeout_, _$mdSidenav_) {
357357
$rootScope = _$rootScope_;
358358
$timeout = _$timeout_;
359+
$mdSidenav = _$mdSidenav_;
359360
}));
360361

361-
it('should find an instantiation using `$mdSidenav(id)`', inject(function($mdSidenav) {
362+
it('should find an instantiation using `$mdSidenav(id)`', function() {
362363
var el = setup('md-component-id="left"');
363364
$timeout.flush();
364365

365366
// Lookup instance still available in the component registry
366367
var instance = $mdSidenav('left');
367368
expect(instance).toBeTruthy();
368-
}));
369+
});
370+
371+
it('should find an instance that contains a data binding', function() {
372+
var leftComponentId = $rootScope.leftComponentId = 'left';
373+
var el = setup('md-component-id="{{ leftComponentId }}"');
374+
var instance = $mdSidenav(leftComponentId, false);
375+
376+
expect(instance).toBeTruthy();
377+
});
369378

370-
it('should find a deferred instantiation using `$mdSidenav(id, true)`', inject(function($mdSidenav) {
379+
it('should find a deferred instantiation using `$mdSidenav(id, true)`', function() {
371380
var instance;
372381

373382
// Lookup deferred (not existing) instance
@@ -386,9 +395,9 @@ describe('mdSidenav', function() {
386395
// Lookup instance still available in the component registry
387396
instance = $mdSidenav('left', true);
388397
expect(instance).toBeTruthy();
389-
}));
398+
});
390399

391-
it('should find a deferred instantiation using `$mdSidenav().waitFor(id)` ', inject(function($mdSidenav) {
400+
it('should find a deferred instantiation using `$mdSidenav().waitFor(id)` ', function() {
392401
var instance;
393402

394403
// Lookup deferred (not existing) instance
@@ -409,9 +418,9 @@ describe('mdSidenav', function() {
409418
instance = $mdSidenav('left');
410419

411420
expect(instance).toBeTruthy();
412-
}));
421+
});
413422

414-
it('should not find a lazy instantiation without waiting `$mdSidenav(id)`', inject(function($mdSidenav) {
423+
it('should not find a lazy instantiation without waiting `$mdSidenav(id)`', function() {
415424
var instance = $mdSidenav('left');
416425
expect(instance.isOpen).toBeDefined(); // returns legacy API with noops
417426

@@ -425,9 +434,9 @@ describe('mdSidenav', function() {
425434
instance = $mdSidenav('left'); // returns instance
426435
expect(instance).toBeDefined();
427436
expect(instance.isOpen()).toBeFalsy();
428-
}));
437+
});
429438

430-
it('should not find a lazy instantiation without waiting `$mdSidenav().find(id)`', inject(function($mdSidenav) {
439+
it('should not find a lazy instantiation without waiting `$mdSidenav().find(id)`', function() {
431440
var instance = $mdSidenav().find('left');
432441
expect(instance).toBeUndefined();
433442

@@ -438,7 +447,7 @@ describe('mdSidenav', function() {
438447
instance = $mdSidenav().find('left');
439448
expect(instance).toBeDefined();
440449
expect(instance.isOpen()).toBeFalsy();
441-
}));
450+
});
442451

443452
describe('onClose', function () {
444453
it('should call callback on escape', inject(function($mdSidenav, $rootScope, $material, $mdConstant, $timeout) {

0 commit comments

Comments
 (0)