2016-11-08 114 views
2

我创建的web应用程序中,我需要从登录重定向到欢迎页面欢迎页面,如果我的用户名和密码验证重定向登录页面从angularjs

<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的脚本,我怎么将页面重定向到另一页面?

回答

1

加$窗口

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

然后添加下面一行在你的回应

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

,你可以使用$位置服务,例如:

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

这里了解更多信息

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> 
+0

感谢杰拉德:) –

0

使用$state的GUID link裁判重定向

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' 
     }) 
}); 
1

你需要在你的控制器$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> 

您应该需要在您的路由提供程序中定义的路径。在您的控制器

(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' 
     }); 
    }]); 
})();