noi.m
noi.m

Reputation: 3132

changing state using $state.go doesn't apply scope

I'm currently working on an ionic angularJs app, where i need to change state on a button click. Apart from numerous other state's i have below two states which are.

.state('app.a', {
    url : "/base/a",
    views : {
        'myContent' : {
            templateUrl : "a.html",
            controller : 'MyController'
        }
    }
})

.state('app.b', {
    url : "/base/b",
    views : {
        'myContent' : {
            templateUrl : "b.html",
            controller : 'MyController'
        }
    }
})

Now in a.html i have a button that calls method clickMe().

$scope.clickMe = function () {
    $state.go('app.b');
    $scope.someVal = 20;
    //$timeout(function(){
    //  $scope.$apply();
    //}, 0);
}

b.html does load, however the value of someVal is just NOT reflected on b.html. One thing to note that if i put $scope.someVal = 20 outside clickMe() it does take effect (however, i need this to happen only on button click).

Not sure, seems to be quite straightforward to me, any help would be appreciated.

Upvotes: 2

Views: 1357

Answers (1)

Kareem Elshahawy
Kareem Elshahawy

Reputation: 1421

Actually, there is a bug in Ionic framework regarding using states with dot notation, also if you must set the value after changing the state you may consider using one of ui-router events, so when the state changed or starting to change you can update the $scope of the controller.

The most important thing is that every time you will create a new instance of the controller, then you must update the value after going to the state you are heading to.

Upvotes: 2

Related Questions