Redirection Ionic

Redirection Ionic

本文关键字:Ionic Redirection      更新时间:2023-09-26

我试图使用Ionic从一个活动移动到另一个活动。我没有任何错误,但这个动作不起作用。

我的状态是:

.config(函数($stateProvider){$stateProvider.state("索引"{templateUrl:"index.html",url:"/"}).state('home'{templateUrl:"templates/welcome.html",url:'/home'});})

我的控制器是:

.controller('HomeCtrl', function($scope, $state, $http, $location) {
    $scope.login = function(user) {
        $http.get("http://localhost/app_dev.php/json/login/"+user.username+"/"+user.password)
        .then(function(response){
            console.log(response.data.status);
            if(response.data.status === 200)
            {
                alert("redirect to main page");
                $location.path("/home");
            }else if(response.data.status === 403){
                alert("Login or password incorrect");
            }else{
                alert("User not found");
            }
        });
    };
})

当我收到Json时,我没有错误,但重定向不会显示

我认为你几乎混淆了状态和位置,我建议保持简单,只使用状态

尝试注入$state并使用它,正如我所看到的,你有一个定义为"Home"的状态,也许这应该做到:

$state.go("home");

而不是使用

$location.path

我希望这能帮助

我遇到了类似的问题,我所做的只是将$event添加到我的html模板中的登录方法中。

<button ng-click="login(user,$event)"></button>

然后在我的js中,我调用了event.prventDefault();

.controller('HomeCtrl', function ($scope, $state, $http, $location) {
    $scope.login = function (user, event) {
        $http.get("http://localhost/app_dev.php/json/login/" + user.username + "/" + user.password)
            .then(function (response) {
                console.log(response.data.status);
                if (response.data.status === 200) {
                    event.preventDefault();
                    alert("redirect to main page");
                    $state.go("home");
                } else if (response.data.status === 403) {
                    alert("Login or password incorrect");
                } else {
                    alert("User not found");
                }
            });
    };
})

$ionicHistory.goBack()应该是最有效的方法。