Reputation: 850
I have an Angular application with different types of pages, all identified by a slug right after the base url, so like:
http://example.com/slug-a
http://example.com/slug-b
Slug a and b are of different types and need to be rendered in a different view. The data belonging to the objects identified by slug a and b is fetched from the server, and the Angular application is supposed to change the view based on the object type.
Is it possible to handle this in the ngRoute's $routeProvider? I can't find any documentation that helps me with this, and I'm sure I'm not the first one to try this..
So, in short:
Different view based on server response.
Upvotes: 0
Views: 254
Reputation: 850
Ok, I've solved it using a different and much simpler approach. (Don't know why I didn't figure this out before)
$routeProvider.when('/:slug/', {
controller: 'SlugController',
template: '<div ng-include="template"></div>'
});
Then the SlugController makes the request and loads the right template by setting $scope.template. In the template file the controller attached to the root element.
Simple but effective.. :)
Upvotes: 0
Reputation: 4166
I think you can not do that in router.
But you can redirect user to a different location via the $location
service.
Like this
// somewhere inside your controller
var promise = loadData().then(data => {
if (data.slugA) {
$location('path_to_slugA');
}
else if (data.slugB) {
$location('path_to_slugB');
}
else {
// do something else
}
});
Upvotes: 0