Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for interpolated ui-view names #1325

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 33 additions & 29 deletions src/viewDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,26 @@
* functionality, call `$uiViewScrollProvider.useAnchorScroll()`.*
*
* @param {string=} onload Expression to evaluate whenever the view updates.
*
*
* @example
* A view can be unnamed or named.
* A view can be unnamed or named.
* <pre>
* <!-- Unnamed -->
* <div ui-view></div>
*
* <div ui-view></div>
*
* <!-- Named -->
* <div ui-view="viewName"></div>
* </pre>
*
* You can only have one unnamed view within any template (or root html). If you are only using a
* You can only have one unnamed view within any template (or root html). If you are only using a
* single view and it is unnamed then you can populate it like so:
* <pre>
* <div ui-view></div>
* <div ui-view></div>
* $stateProvider.state("home", {
* template: "<h1>HELLO!</h1>"
* })
* </pre>
*
*
* The above is a convenient shortcut equivalent to specifying your view explicitly with the {@link ui.router.state.$stateProvider#views `views`}
* config property, by name, in this case an empty name:
* <pre>
Expand All @@ -54,33 +54,33 @@
* "": {
* template: "<h1>HELLO!</h1>"
* }
* }
* }
* })
* </pre>
*
* But typically you'll only use the views property if you name your view or have more than one view
* in the same template. There's not really a compelling reason to name a view if its the only one,
*
* But typically you'll only use the views property if you name your view or have more than one view
* in the same template. There's not really a compelling reason to name a view if its the only one,
* but you could if you wanted, like so:
* <pre>
* <div ui-view="main"></div>
* </pre>
* </pre>
* <pre>
* $stateProvider.state("home", {
* views: {
* "main": {
* template: "<h1>HELLO!</h1>"
* }
* }
* }
* })
* </pre>
*
*
* Really though, you'll use views to set up multiple views:
* <pre>
* <div ui-view></div>
* <div ui-view="chart"></div>
* <div ui-view="data"></div>
* <div ui-view="chart"></div>
* <div ui-view="data"></div>
* </pre>
*
*
* <pre>
* $stateProvider.state("home", {
* views: {
Expand All @@ -93,7 +93,7 @@
* "data": {
* template: "<data_thing/>"
* }
* }
* }
* })
* </pre>
*
Expand All @@ -111,8 +111,8 @@
* <ui-view autoscroll='scopeVariable'/>
* </pre>
*/
$ViewDirective.$inject = ['$state', '$injector', '$uiViewScroll'];
function $ViewDirective( $state, $injector, $uiViewScroll) {
$ViewDirective.$inject = ['$state', '$injector', '$uiViewScroll', '$uiViewName'];
function $ViewDirective( $state, $injector, $uiViewScroll, $uiViewName) {

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

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

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

$ViewDirectiveFill.$inject = ['$compile', '$controller', '$state'];
function $ViewDirectiveFill ($compile, $controller, $state) {
$ViewDirectiveFill.$inject = ['$compile', '$controller', '$state', '$uiViewName'];
function $ViewDirectiveFill ($compile, $controller, $state, $uiViewName) {
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 = $uiViewName(attrs, $element.inheritedData('$uiView'), scope),
locals = current && current.locals[name];

if (! locals) {
Expand Down Expand Up @@ -283,13 +283,17 @@ function $ViewDirectiveFill ($compile, $controller, $state) {
}

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

angular.module('ui.router.state').factory('$uiViewName', $UiViewName);
angular.module('ui.router.state').directive('uiView', $ViewDirective);
angular.module('ui.router.state').directive('uiView', $ViewDirectiveFill);