[英]Redirect to another page with Angular
我是Angular的新手,所以我想對這個問題表示歉意。
我有這個發送POST請求的Angular腳本,在“失敗”部分中,我想將用戶重定向到另一個頁面。 這是網站的結構
-登錄文件夾
js文件夾
controllers.js
login.html
login.css
-index.html
我在controllers.js中的Angular代碼ID。 我使用login.html中的代碼。 我需要將用戶從login.html重定向到index.html。 不幸的是,這不起作用,當它進入代碼的錯誤部分時,它不會重定向用戶。
這是代碼:
var app = angular.module('WebApp', []);
app.controller('myCtrl', function($scope, $http, $location) {
$scope.login = function($location){
$scope.incorrectPass = false;
$http.post('service link', {'email': $scope.username, 'password': $scope.password, 'UUID': 'dknasklaxcaosdhdajkshnjk', 'platform': 1})
.success(function(data, status, headers, config) {
alert("request success");
})
.error(function(data, status) {
$scope.$apply(function() { $location.path("/index.html"); });
});
}
});
我希望在您的routeProvider或stateProvide中您有到達index.html的路由。 它永遠不會這樣工作。 您應該具有前往.html頁面的路線
.config(['$routeProvider', function ($routeProvider) {
$routeProvider
.when("/home", {
title: 'home',
templateUrl: 'index.html',
controller: 'HomeController as homeCtrl'
})
.otherwise({
redirectTo: '/login'
});
現在,如果你這樣做
$location.path("/home");
在您的login / js / controller.js文件中,它將正常工作。
使用此代碼重定向$ window.location.href ='/index.html';
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.