将登录页面重定向到angularjs的欢迎页面

时间:2016-11-08 08:56:18

标签: javascript angularjs asp.net-mvc-5

我正在创建一个网络应用程序,如果我的用户ID和密码已经过验证,我需要从登录重定向到欢迎页面

    <script>
    var app = angular.module('myApp', []);
    app.controller('customersCtrl', function ($scope, $http, $interval) {
        //login
        $scope.loginbutton = function () {
            $http.get('/login.asmx/loginuser', {
                params: {
                    log: $scope.log,
                    pm: $scope.pm,
                    password: $scope.password
                }
            })
            .then(function (response) {
                {
                   //here i want to redirect to another page
                }
            })
        }
    });
</script>

这是我的angularjs脚本如何将页面重定向到另一个页面?

5 个答案:

答案 0 :(得分:1)

您可以使用$ location服务,例如:

<br>$location.path('/home')<br>

这里是ref guid link 以获取更多信息

答案 1 :(得分:0)

<script>
    var app = angular.module('myApp', ['$location']);
    app.controller('customersCtrl', function ($scope, $http, $interval, $location) { // add $location dependency in the 
        //login
        $scope.loginbutton = function () {
            $http.get('/login.asmx/loginuser', {
                params: {
                    log: $scope.log,
                    pm: $scope.pm,
                    password: $scope.password
                }
            })
            .then(function (response) {
                {
                    $location.path('google.com') //pass the url in it.
                }
            })
        }
    });
</script>

答案 2 :(得分:0)

使用$state重定向

controller.js

controller('customersCtrl', function ($scope, $http, $interval, $state) {
    $scope.loginbutton = function () {
        $http.get('/login.asmx/loginuser', {
            params: {
                log: $scope.log,
                pm: $scope.pm,
                password: $scope.password
            }
        })
        .then(function (response) {
             $state.go('page');
        })
    }
});

然后在您的route.js中,您需要指定要重定向到的新状态。

angular.module('app.routes', []);
    .config(function($stateProvider, $urlRouterProvider) {

    $stateProvider

        .state('page', {
            url: '/page',
            templateUrl: 'templates/page.html',
            controller: 'pageCtrl'
       })
});

答案 3 :(得分:0)

您需要在控制器中包含$location依赖项,并且需要调用$location.url('/login')来导航到登录页面。我为此修改了你的代码。

<script>
    var app = angular.module('myApp', []);
    app.controller('customersCtrl', function ($scope, $http, $interval, $location) {
        //login
        $scope.loginbutton = function () {
            $http.get('/login.asmx/loginuser', {
                params: {
                    log: $scope.log,
                    pm: $scope.pm,
                    password: $scope.password
                }
            })
            .then(function (response) {
                {
                   //Navigate to welcome page
                   $location.url('/welcome');
                }
            })
        }
    });
</script>

您需要在路由提供商中定义welcome路径,例如。

(function () {
    'use strict';

    angular.module('myApp', [
        'ngRoute'
    ])

    .config(['$routeProvider', function ($routeProvider) {
        $routeProvider.when('/login', {
            templateUrl: 'views/landing.html',
            controller: 'landingController'
        });
        $routeProvider.when('/customers', {
            templateUrl: 'views/customers.html',
            controller: 'customersCtrl'
        });
        $routeProvider.when('/welcome', {
            templateUrl: 'views/welcome.html',
            controller: 'welcomeController'
        });
        ..............
        ..............
        $routeProvider.otherwise({
            redirectTo: '/login'
        });
    }]);
})();

答案 4 :(得分:0)

在控制器中添加$ window

app.controller('customersCtrl', function ($scope, $http, $interval, $location, $window) {

然后在您的回复中添加以下行

.then(function (response) {
                    {
                        $window.location.href = "../welcome/Index";
                    }
                })