Skip to content

Commit

Permalink
fix(uiView): allow inteprolated ui-view names
Browse files Browse the repository at this point in the history
Made internal function getUiViewName allow interpolated values.
Added test for issue #1324

Closes #1324
  • Loading branch information
christopherthielen committed Sep 14, 2014
1 parent c3bb7ad commit 81f6a19
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/viewDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@
* <ui-view autoscroll='scopeVariable'/>
* </pre>
*/
$ViewDirective.$inject = ['$state', '$injector', '$uiViewScroll'];
function $ViewDirective( $state, $injector, $uiViewScroll) {
$ViewDirective.$inject = ['$state', '$injector', '$uiViewScroll', '$interpolate'];
function $ViewDirective( $state, $injector, $uiViewScroll, $interpolate) {

function getService() {
return ($injector.has) ? function(service) {
Expand Down Expand Up @@ -209,7 +209,7 @@ function $ViewDirective( $state, $injector, $uiViewScroll) {

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

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

$ViewDirectiveFill.$inject = ['$compile', '$controller', '$state'];
function $ViewDirectiveFill ($compile, $controller, $state) {
$ViewDirectiveFill.$inject = ['$compile', '$controller', '$state', '$interpolate'];
function $ViewDirectiveFill ( $compile, $controller, $state, $interpolate) {
return {
restrict: 'ECA',
priority: -400,
compile: function (tElement) {
var initial = tElement.html();
return function (scope, $element, attrs) {
var current = $state.$current,
name = getUiViewName(attrs, $element.inheritedData('$uiView')),
name = getUiViewName(scope, attrs, $element, $interpolate),
locals = current && current.locals[name];

if (! locals) {
Expand Down Expand Up @@ -290,10 +290,11 @@ function $ViewDirectiveFill ($compile, $controller, $state) {

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

Expand Down
30 changes: 30 additions & 0 deletions test/viewDirectiveSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,36 @@ describe('uiView', function () {
expect(uiViews.eq(1).text()).toBe(lState.views.view2.template);
expect(uiViews.eq(2).text()).toBe(lState.views.view3.template);
}));

it ('should interpolate ui-view names', inject(function($state, $q, $compile) {
elem.append($compile('<div ng-repeat="view in views">' +
'<ui-view name="view{{$index + 1}}">hallo</ui-view>' +
'</div>')(scope));

$state.transitionTo(lState);
$q.flush();

expect(elem.find('ui-view').length).toBe(0);

scope.views = ['view1', 'view2'];

scope.$digest();

var uiViews = elem.find('ui-view');

expect(uiViews.eq(0).text()).toBe(lState.views.view1.template);
expect(uiViews.eq(1).text()).toBe(lState.views.view2.template);
expect(uiViews.eq(2).length).toBe(0);

scope.views.push('view3');
scope.$digest();

uiViews = elem.find('ui-view');

expect(uiViews.eq(0).text()).toBe(lState.views.view1.template);
expect(uiViews.eq(1).text()).toBe(lState.views.view2.template);
expect(uiViews.eq(2).text()).toBe(lState.views.view3.template);
}));
});
});

Expand Down

0 comments on commit 81f6a19

Please sign in to comment.