Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I pass multiple controllers in $routeProvider.when() in angularJS?

I tried searching for this on various threads, but I can't conclusively understand this.

test.config(['$routeProvider', function($routeProvider){
$routeProvider
    .when('/',
        {
            controller:'SimpleController1',
            templateUrl: 'partials/1.html'
        })
    .when('/xyz',
        {
            controller:'SimpleController1, SimpleController2',
            templateUrl:'partials/2.html'
        })
    .otherwise({ redirectTo: '/'});
}]);

I tried doing the above snippet, but it's not working. Can I do something like this? If yes, then what is it that I'm doing wrong here?

like image 646
Kshitij Avatar asked Jul 31 '14 14:07

Kshitij


People also ask

Can we have multiple controllers in AngularJS?

An AngularJS application can contain one or more controllers as needed, in real application a good approach is to create a new controller for every significant view within the application.

What is the role of routeProvider in AngularJS?

Routing is allows us create Single Page Applications.To do this, we use ng-view and ng-template directives, and $routeProvider services. We use $routeProvider to configure the routes.

How to use ngRoute in AngularJS?

Then you must add the ngRoute as a dependency in the application module: var app = angular. module("myApp", ["ngRoute"]); Now your application has access to the route module, which provides the $routeProvider .

How routing works in AngularJS?

Routing in AngularJS is used when the user wants to navigate to different pages in an application but still wants it to be a single page application. AngularJS routes enable the user to create different URLs for different content in an application.

What is $routeprovider in Angular 2?

$routeProvider is a simple API which which accepts either when () or otherwise () method. We need to install the ngRoute module. $routeProvider is a function under config (mainApp module) using the key as ‘$routeProvider’. $routeProvider.when defines the URL “/addStudent”.

What is the use of custom routing in AngularJS?

Routing is used to present different views to the user on the same web page. This is basically the concept used in Single page applications which are implemented for almost all modern day web applications A default route can be set-up for angular.JS routing.

How to access parameters from the route in angular?

Accessing parameters from the route. Angular also provides the functionality to provide parameters during routing. The parameters are added to the end of the route in the URL, for example, http://guru99/index.html#/Angular/1. The # symbol is the separator between the main application URL and the route.

What is ngroute in AngularJS?

The ngRoute module routes your application to different pages without reloading the entire application. What do I Need? To make your applications ready for routing, you must include the AngularJS Route module: Then you must add the ngRoute as a dependency in the application module:


1 Answers

Only one controller is allowed and will be assigned to the loading template as the controller in ng-view. No need to define ng-controller in the template for a main controller.

If you need to define multiple controllers I suggest you define one main/parent controller and use that in the routeProvider and then have others already in the template using the ng-controller directive.

or...

Check into using Angular UI's UI-Router : http://angular-ui.github.io/ which is a much more versatile router.

like image 200
m.e.conroy Avatar answered Oct 13 '22 12:10

m.e.conroy