0
votes

I trying to move from an activity to an other using Ionic. I dont have any error but the action does not work.

My state is :

.config(function ($stateProvider) { $stateProvider .state('index', { templateUrl: 'index.html', url: '/' }) .state('home', { templateUrl: 'templates/welcome.html', url: '/home' }); })

My controller is :

.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");
            }
        });
    };
})

When i receve my Json, I do not have error but the redirection does not show

3
are you using UI Router? - an earwig

3 Answers

0
votes

I had a problem similar to this and all i did was add the $event to my login method in my html template.

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

Then in my js I called event.preventDefault();

.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");
                }
            });
    };
})
1
votes

I think you are pretty much mixing up states and locations, I'd suggest to keep it simple and use states only.

try to inject $state and use it, as I can see you have a state defined as "Home", maybe this should do the trick:

$state.go("home");

instead of using

$location.path

I hope that helps

0
votes

$ionicHistory.goBack() should be the most effective way, I suppose.