使用$ routeProvider

时间:2019-06-09 17:57:09

标签: angularjs asp.net-mvc ngroute

我的AngularJs路由始终重定向到主页并始终调用HomeController而不是重定向到/VechileRegistration/IndexVechileRegistration/VechileInward

$routeProvider
  .when('/', {
    controller: 'HomeController',
    templateUrl: '/home/home.view.html',
    controllerAs: 'vm'
  })
  .when('/VechileRegistration/Index', {
    controller: 'HomeController2',
    templateUrl: '/home/home.view2.html',
    controllerAs: 'vm'
  })
  .when('/VechileRegistration/VechileInward', {
    controller: 'VehicleInwardController',
    templateUrl: '/home/VehicleInward.view.html',
    controllerAs: 'vm'
  })
  .otherwise({
    redirectTo: '/login'
  });

如何重定向到正确的位置并调用适当的控制器?

1 个答案:

答案 0 :(得分:0)

        var app = angular.module('ngRoutingDemo', ['ngRoute']);

    app.config(function ($routeProvider) {
         
        $routeProvider.when('/', {
            templateUrl: '/login.html',
            controller: 'loginController'
        }).when('/student/:username', {
            templateUrl: '/student.html',
            controller: 'studentController'
        }).otherwise({
            redirectTo: "/"
        });

    app.controller("loginController", function ($scope, $location) {
       
        $scope.authenticate = function (username) {
            // write authentication code here.. 

            $location.path('/student/' + username)
        };

    });

    app.controller("studentController", function ($scope, $routeParams) {
        $scope.username = $routeParams.username;
    });

});