簡體   English   中英

如何使用Angular Js重定向頁面?

[英]How to redirect Page using Angular Js?

我想知道如何使用Angular Js重定向到另一個頁面。

我已經在這里關注了幾個問題,並且沒有找到任何成功的答案

這是我的代碼:

var app = angular.module('formExample',[]);
app.controller('formCtrl',function($scope,$http){    
    $scope.insertData=function(){      

      //  if($scope.name =='' && $scope.email == '' && $scope.message = '' && $scope.price =='' && $scope.date == null && $scope.client == ''){return;}
        $http.post("/php/login.php", {
           "email": $scope.email, "password": $scope.password
        }).then(function(response, $location){
                alert("Login Successfully");
                $scope.email = '';
                $scope.password = '';
                $location.path('/clients');

            },function(error){
                alert("Sorry! Data Couldn't be inserted!");
                console.error(error);

            });
        }
    });

我收到此錯誤:

TypeError: Cannot read property 'path' of undefined

您需要向控制器注入$location

app.controller('formCtrl',function($scope,$http,$location){    

你可以使用普通的JS

window.location.href = '/my-other-page'

或者,如果您使用'ui-router'

$state.reload()

要么

$state.go($state.current.name, {}, {reload: true})

不要忘記將$ state注入您的控制器依賴項:

app.controller('formCtrl',function($scope, $http, $state){ 

您可以使用控制器中的$ window重定向。

$window.location.href = '/index.html';

希望這對你有所幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM