Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Multiple named ngView directives and attaching views to routes #1198

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
30 changes: 27 additions & 3 deletions src/ng/directive/ngView.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,40 @@ var ngViewDirective = ['$http', '$templateCache', '$route', '$anchorScroll', '$c
destroyLastScope();
}

function update() {
var locals = $route.current && $route.current.locals,
function update(event, next, last) {
var currentRoute = $route.current,
locals = currentRoute && currentRoute.locals,
template = locals && locals.$template;

if ( ! currentRoute && attr.ngView) {
return;
}

if (currentRoute && currentRoute.$route.view && ! attr.ngView) {
return;
}

if (attr.ngView && currentRoute && ! currentRoute.$route.view) {
scope.$broadcast('$viewContentCleared');
clearContent();
}

if (currentRoute && attr.ngView && ( ! currentRoute.$route.view || attr.ngView !== currentRoute.$route.view)) {
return;
}

if (last && attr.ngView && (last.$route.templateUrl != next.$route.templateUrl || last.$route.view != next.$route.view)) {
$route.last = last;
} else {
$route.last = null;
}

if (template) {
element.html(template);
destroyLastScope();

var link = $compile(element.contents()),
current = $route.current,
current = currentRoute,
controller;

lastScope = current.scope = scope.$new();
Expand Down
60 changes: 40 additions & 20 deletions src/ng/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ function $RouteProvider(){
* If the option is set to `false` and url in the browser changes, then
* `$routeUpdate` event is broadcasted on the root scope.
*
* - `view` - `{string}`: a name of the view associated with the route.
* Useful for multiple views. If no view is specified then all of the views
* with no name are affected from the $routeChangeSuccess event.
*
* @returns {Object} self
*
* @description
Expand Down Expand Up @@ -305,6 +309,41 @@ function $RouteProvider(){
reload: function() {
forceReload = true;
$rootScope.$evalAsync(updateRoute);
},

/**
* @ngdoc method
* @name ng.$route#interpolate
* @methodOf ng.$route
*
* @description
*
* Interpolate a path with params.
*
* Used to interpolate `redirectTo` path.
* Exposed to be used for manually getting a path
* from a route object.
*
* Example usage:
* routePath = '/path/:someParam';
* $route.interpolate(routePath, routeObject.params);
*
* @returns interpolation of a path with the parameters
*/
interpolate: function(string, params) {
var result = [];
forEach((string||'').split(':'), function(segment, i) {
if (i === 0) {
result.push(segment);
} else {
var segmentMatch = segment.match(/(\w+)(.*)/);
var key = segmentMatch[1];
result.push(params[key]);
result.push(segmentMatch[2] || '');
delete params[key];
}
});
return result.join('');
}
};

Expand Down Expand Up @@ -354,7 +393,7 @@ function $RouteProvider(){
if (next) {
if (next.redirectTo) {
if (isString(next.redirectTo)) {
$location.path(interpolate(next.redirectTo, next.params)).search(next.params)
$location.path($route.interpolate(next.redirectTo, next.params)).search(next.params)
.replace();
} else {
$location.url(next.redirectTo(next.pathParams, $location.path(), $location.search()))
Expand Down Expand Up @@ -427,24 +466,5 @@ function $RouteProvider(){
// No route matched; fallback to "otherwise" route
return match || routes[null] && inherit(routes[null], {params: {}, pathParams:{}});
}

/**
* @returns interpolation of the redirect path with the parametrs
*/
function interpolate(string, params) {
var result = [];
forEach((string||'').split(':'), function(segment, i) {
if (i == 0) {
result.push(segment);
} else {
var segmentMatch = segment.match(/(\w+)(.*)/);
var key = segmentMatch[1];
result.push(params[key]);
result.push(segmentMatch[2] || '');
delete params[key];
}
});
return result.join('');
}
}];
}