gpt4 book ai didi

html - $window.location.href 在 AngularJS 中不工作

转载 作者:太空狗 更新时间:2023-10-29 13:46:16 25 4
gpt4 key购买 nike

我正在构建一个基本的 AngularJS 登录页面并且 $window.location.href 没有将页面重定向到我系统中的新 html,托管通过 WAMP。我什至尝试将其重定向到谷歌。什么都没发生。我在这里尝试了所有可用的解决方案,但似乎没有任何效果。任何解决方案?

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>

最佳答案

在 Angular js 中你可以使用 $location。将其注入(inject)您的 Controller :

app.controller('MainCtrl', function($scope, $location) { ... }

如果您想重定向到 google,请使用其 url() 方法:

$location.url('http://google.fr');

你也可以使用 path() 方法获取相对 url :

$location.path('home'); // will redirect you to 'yourDomain.xx/home'

https://docs.angularjs.org/api/ng/service/ $地点

关于html - $window.location.href 在 AngularJS 中不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34017904/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com