Skip to content

Commit 81f6a19

Browse files
fix(uiView): allow inteprolated ui-view names
Made internal function getUiViewName allow interpolated values. Added test for issue #1324 Closes #1324
1 parent c3bb7ad commit 81f6a19

File tree

2 files changed

+40
-9
lines changed

2 files changed

+40
-9
lines changed

src/viewDirective.js

+10-9
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@
111111
* <ui-view autoscroll='scopeVariable'/>
112112
* </pre>
113113
*/
114-
$ViewDirective.$inject = ['$state', '$injector', '$uiViewScroll'];
115-
function $ViewDirective( $state, $injector, $uiViewScroll) {
114+
$ViewDirective.$inject = ['$state', '$injector', '$uiViewScroll', '$interpolate'];
115+
function $ViewDirective( $state, $injector, $uiViewScroll, $interpolate) {
116116

117117
function getService() {
118118
return ($injector.has) ? function(service) {
@@ -209,7 +209,7 @@ function $ViewDirective( $state, $injector, $uiViewScroll) {
209209

210210
function updateView(firstTime) {
211211
var newScope,
212-
name = getUiViewName(attrs, $element.inheritedData('$uiView')),
212+
name = getUiViewName(scope, attrs, $element, $interpolate),
213213
previousLocals = name && $state.$current && $state.$current.locals[name];
214214

215215
if (!firstTime && previousLocals === latestLocals) return; // nothing to do
@@ -251,16 +251,16 @@ function $ViewDirective( $state, $injector, $uiViewScroll) {
251251
return directive;
252252
}
253253

254-
$ViewDirectiveFill.$inject = ['$compile', '$controller', '$state'];
255-
function $ViewDirectiveFill ($compile, $controller, $state) {
254+
$ViewDirectiveFill.$inject = ['$compile', '$controller', '$state', '$interpolate'];
255+
function $ViewDirectiveFill ( $compile, $controller, $state, $interpolate) {
256256
return {
257257
restrict: 'ECA',
258258
priority: -400,
259259
compile: function (tElement) {
260260
var initial = tElement.html();
261261
return function (scope, $element, attrs) {
262262
var current = $state.$current,
263-
name = getUiViewName(attrs, $element.inheritedData('$uiView')),
263+
name = getUiViewName(scope, attrs, $element, $interpolate),
264264
locals = current && current.locals[name];
265265

266266
if (! locals) {
@@ -290,10 +290,11 @@ function $ViewDirectiveFill ($compile, $controller, $state) {
290290

291291
/**
292292
* Shared ui-view code for both directives:
293-
* Given attributes and inherited $uiView data, return the view's name
293+
* Given scope, element, and its attributes, return the view's name
294294
*/
295-
function getUiViewName(attrs, inherited) {
296-
var name = attrs.uiView || attrs.name || '';
295+
function getUiViewName(scope, attrs, element, $interpolate) {
296+
var name = $interpolate(attrs.uiView || attrs.name || '')(scope);
297+
var inherited = element.inheritedData('$uiView');
297298
return name.indexOf('@') >= 0 ? name : (name + '@' + (inherited ? inherited.state.name : ''));
298299
}
299300

test/viewDirectiveSpec.js

+30
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,36 @@ describe('uiView', function () {
420420
expect(uiViews.eq(1).text()).toBe(lState.views.view2.template);
421421
expect(uiViews.eq(2).text()).toBe(lState.views.view3.template);
422422
}));
423+
424+
it ('should interpolate ui-view names', inject(function($state, $q, $compile) {
425+
elem.append($compile('<div ng-repeat="view in views">' +
426+
'<ui-view name="view{{$index + 1}}">hallo</ui-view>' +
427+
'</div>')(scope));
428+
429+
$state.transitionTo(lState);
430+
$q.flush();
431+
432+
expect(elem.find('ui-view').length).toBe(0);
433+
434+
scope.views = ['view1', 'view2'];
435+
436+
scope.$digest();
437+
438+
var uiViews = elem.find('ui-view');
439+
440+
expect(uiViews.eq(0).text()).toBe(lState.views.view1.template);
441+
expect(uiViews.eq(1).text()).toBe(lState.views.view2.template);
442+
expect(uiViews.eq(2).length).toBe(0);
443+
444+
scope.views.push('view3');
445+
scope.$digest();
446+
447+
uiViews = elem.find('ui-view');
448+
449+
expect(uiViews.eq(0).text()).toBe(lState.views.view1.template);
450+
expect(uiViews.eq(1).text()).toBe(lState.views.view2.template);
451+
expect(uiViews.eq(2).text()).toBe(lState.views.view3.template);
452+
}));
423453
});
424454
});
425455

0 commit comments

Comments
 (0)