2

I am using angularJs for my webApp, but i used $stateProvider everywhere to define state.so now I want to use $routeProvider instead of $stateProvider.and how I change the route? is $location is good to change the path ? This is my trial code with $routeProvider

angular.module("angular.states", [])
    .config(['$routeProvider', '$locationProvider', '$stateProvider'], function ($stateProvider, templateRoot, $routeProvider) {
        $routeProvider.when('/login?username&message&error', {
            'templateUrl': '/templates/login.html',
            'controller': 'LoginController'
        });
        $routeProvider.when('/logout', {
            'templateUrl': '/templates/logout.html',
            'controller': 'LogoutController'
        });
    })
    .controller('LoginController', function (UserService, $scope, $state, $stateParams, AuthFactory, $timeout, $location) {
        $scope.login = function (username, password) {
            UserService.login({
                'username': username,
                'password': password
            });
        };
    })
    .controller('LogoutController', function (UserService, $scope, $location) {
        $scope.logout = function () {
            UserService.logout({}, function () {
                $location.path("/login", {
                });
            });
        };
    });

I am not sure about $routeProvider, so please tell me how to use it

ojus kulkarni
  • 1,877
  • 3
  • 25
  • 41

1 Answers1

2

You should use $stateProvider instead of $routeProvider. because you can easily change your state using $stateProvider. & if you use $routeProvider you need to use location, path, its url & all, You cannot relate to routes with each other like who is parent and who is child. so $stateProvider is always better to use.

Using $stateProvider :

$stateProvider.state('login', {
        'url': '/login
        'templateUrl': '/templates/login.html',
        'controller': 'LoginController'
    });

here you can simply change your state with given url you dont need to locate the path.

Kunal Gadhia
  • 350
  • 2
  • 14