我正在构建一个基本的AngularJS登录页面,$window.location.href没有将页面重定向到我的系统中由WAMP托管的新html.我甚至尝试将它重新指向谷歌.什么都没发生.我在这里尝试了所有可用的解决方案,似乎没有任何效果.有解决方案?
JS后跟 HTML
JS后跟 HTML
var app = angular.module('myApp',[]); app.controller('MainCtrl',function($scope) { $scope.name = 'World'; $scope.submit = function() { if ($scope.username && $scope.password) { var user = $scope.username; var pass = $scope.password; if (pass == "admin" && user == "admin@admin.com") { alert("Login Successful"); $window.location.href = "http://google.com"; //Re-direction to some page } else if (user != "admin@admin.com") { alert("Invalid username"); } else if (pass != "admin" && user == "admin@admin.com") { alert("Invalid password"); } } else { alert("Invalid Login"); } } });
<!DOCTYPE html> <html ng-app="myApp"> <head> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> <script src="login.js"></script> <link rel="stylesheet" href="login.css"> </head> <body ng-controller="MainCtrl"> <form> <label>Username:</label> <input type="email" ng-model="username" class="lab1" /> </br> </br> <label></label>Password:</label> <input type="password" ng-model="password" class="lab2"> </br> <button type="submit" ng-click="submit()" class="buttonclass">login</button> </form> </body> </html>
解决方法
在角度js中,您可以使用$location.将其注入您的控制器:
app.controller('MainCtrl',function($scope,$location) { ... }
$location.url('http://google.fr');
你也可以使用相对url的path()方法:
$location.path('home'); // will redirect you to 'yourDomain.xx/home'