Reputation: 1493
The AngularJs documentation says you can specify either a string or a function for controller & templateUrl in the route object when configuring the $routeProvider, however I'm running into an issue when using a function to dynamically determine the controller based on $location parameters. I have the following route configuration:
$routeProvider
.when( '/workspace/:workspaceId/product/:productId/item/:itemType/:itemId/edit', {
templateUrl: function ( param ) {
switch ( param.itemType ) {
case 'topic':
return 'topic.tpl.html';
case 'course':
return 'course.tpl.html';
}
throw new Error( "Unknown product item type '" + param.itemType + "'" );
},
controller: function ( param ) {
switch ( param.itemType ) {
case 'topic':
return 'TopicController';
case 'course':
return 'CourseController';
}
throw new Error( "Unknown product item type '" + param.itemType + "'" );
}
} );
When loading the application I get the following error:
Error: [$injector:unpr] Unknown provider: paramProvider <- param
Am I missing anything obvious here? Switching the controller to use a string rather than a function fixes the issue.
Looks like some people have run into this issue before (eg. here), but a I'm confused why this doesn't work as the documentation suggests that it should.
Any help would be greatly appreciated!
Joseph.
Upvotes: 0
Views: 1116
Reputation: 3569
I think part of the issue may be that when you supply a function rather than a string to controller, it's not a function that's meant to return a controller name - it's instead a function that's meant to behave as the controller.
Have a glance at the following - the route/dynamic controller behaviour seems to work. I'm using $controller
to instantiate the controller by name. So in this case, I'm not actually dynamically returning a different controller, I'm instantiating a new controller within the anon controller I've supplied to the $routeProvider
.
Whether or not this is the best way to accomplish your intended end result might be a different matter. ;) The angular-ui/ui-router project is quite popular.
angular.module('myApp', ['ngRoute'])
.config(function($routeProvider) {
$routeProvider
.when('/workspace/:workspaceId/product/:productId/item/:itemType/:itemId/edit', {
template: '<h1>{{title}}</h1><p>{{test}}</p>',
controller: function($scope, $routeParams, $controller) {
switch ($routeParams.itemType) {
case 'topic':
$controller('TopicController', {
$scope: $scope
});
break;
case 'course':
$controller('CourseController', {
$scope: $scope
});
break;
}
}
})
.when('/', {
template: '<h1>Home</h1><p>{{test}}</p>',
controller: 'home'
});
}).controller('home', function($scope) {
$scope.test = 'Hello world.';
}).controller('TopicController', function($scope) {
$scope.title = "Topic";
$scope.test = 'Success!';
}).controller('CourseController', function($scope) {
$scope.title = "Course";
$scope.test = 'Success again!';
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.27/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.27/angular-route.min.js"></script>
<div ng-app="myApp">
<a href="#/workspace/1/product/1/item/topic/1/edit">Topic</a>
<a href="#/workspace/1/product/1/item/course/1/edit">Course</a>
<div ng-view />
</div>
Upvotes: 1