Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using $routeProvider with $stateProvider

in beginning I was just using $routeProvider as follows and it was giving me what I wanted.

angular.module('angularProject', ['angularProject.filters', 'angularProject.services', 'angularProject.directives', 'angularProject.controllers'])
  .config(['$routeProvider', function($routeProvider) {
    $routeProvider
      .when('/home', {
        title: 'Home',
        templateUrl: 'partials/home.html',
        controller: 'homeCtrl'
      })
      .otherwise({redirectTo: '/home'});
  }]);

But when I also wanted to use $stateProvider as follows it is not giving me errors but also not allowing me to change state..

var myapp=angular.module('angularProject', ['ui.bootstrap','ui.router','angularProject.filters', 'angularProject.services', 'angularProject.directives', 'angularProject.controllers'])
  myapp.config(['$routeProvider', '$stateProvider',function($routeProvider,$stateProvider) {
    $routeProvider
      .when('/home', {
        title: 'Home',
        templateUrl: 'views/home.html',
        controller: 'homeCtrl'
      })
      .otherwise('/home');
    $stateProvider
        // .state('index', {
            // url: "",
            // views: {
                // "viewA": {
                    // template: "index.viewA"
                // },
                // "viewB": {
                    // template: "index.viewB"
                // }
            // }
        // })
        .state('route1', {
            url: "/route1",
            views: {
                "viewA": {
                    template: "route1.viewA"
                },
                "viewB": {
                    template: "route1.viewB"
                }
            }
        })
        .state('route2', {
            url: "/route2",
            views: {
                "viewA": {
                    template: "route2.viewA"
                },
                "viewB": {
                    template: "route2.viewB"
                }
            }
        })
     // .otherwise({redirectTo: '/home'});
  }]);

Any help on how to use $stateProvider with $routeProvider would be appreciated..

like image 645
pawan9977 Avatar asked Nov 10 '13 10:11

pawan9977


People also ask

What is$ routeProvider?

The $routeProvider creates the $route service. By configuring the $routeProvider before the $route service we can set routes in HTML templates which will be displayed. The $routeProvider is configured with the help of calls to the when() and otherwise() functions.

What is $stateProvider in AngularJS?

$stateProvider is used to define different states of one route. You can give the state a name, different controller, different view without having to use a direct href to a route. There are different methods that use the concept of $stateprovider in AngularJS.

What is ngRoute?

If you want to navigate to different pages in your application, but you also want the application to be a SPA (Single Page Application), with no page reloading, you can use the ngRoute module. The ngRoute module routes your application to different pages without reloading the entire application.


1 Answers

I don't think it's possible and I don't think that ui.router was built to be used with ngRoute.
According to the ui-router docs, you should use ui-router's own implementation of $routeProvider, called $urlRouterProvider.

like image 125
gion_13 Avatar answered Nov 15 '22 01:11

gion_13