how to access variable of controller in the run method












0















In my project when user is coming from any url then first i check in the controller that what was the last status of the user if they coming on the page which was not accessible yet then i am set a $rootScope variable to the current status of the user and use this variable in app's run method and send them to state according to there current status in $rootScope which I set in controller, but my issue is that the function which is getting the current status from the nodejs's api is calling after the checking apply in the run method, so the result is in the run method current status in showing undefined because the controller function is running after the code of run method. I not able to understand how to solve this problem. Please, I request you that help me to solve this problem.




Here is the code of the function of the controller which set the $rootScope variable




$scope.getUserDetails = function () {

appService.getDetails('/user/getDetails').then(function (response) {
$scope.current_status = response.current_status;
$rootScope.current_status = $scope.current_status;
}
}



And here is the code of run method




if (transition.to().name == "user.stateName"){
console.log($rootScope.current_status);
$state.go('store.' + $rootScope.current_status);
}



Output of the $rootScope.current_status in console is




undefined









share|improve this question



























    0















    In my project when user is coming from any url then first i check in the controller that what was the last status of the user if they coming on the page which was not accessible yet then i am set a $rootScope variable to the current status of the user and use this variable in app's run method and send them to state according to there current status in $rootScope which I set in controller, but my issue is that the function which is getting the current status from the nodejs's api is calling after the checking apply in the run method, so the result is in the run method current status in showing undefined because the controller function is running after the code of run method. I not able to understand how to solve this problem. Please, I request you that help me to solve this problem.




    Here is the code of the function of the controller which set the $rootScope variable




    $scope.getUserDetails = function () {

    appService.getDetails('/user/getDetails').then(function (response) {
    $scope.current_status = response.current_status;
    $rootScope.current_status = $scope.current_status;
    }
    }



    And here is the code of run method




    if (transition.to().name == "user.stateName"){
    console.log($rootScope.current_status);
    $state.go('store.' + $rootScope.current_status);
    }



    Output of the $rootScope.current_status in console is




    undefined









    share|improve this question

























      0












      0








      0








      In my project when user is coming from any url then first i check in the controller that what was the last status of the user if they coming on the page which was not accessible yet then i am set a $rootScope variable to the current status of the user and use this variable in app's run method and send them to state according to there current status in $rootScope which I set in controller, but my issue is that the function which is getting the current status from the nodejs's api is calling after the checking apply in the run method, so the result is in the run method current status in showing undefined because the controller function is running after the code of run method. I not able to understand how to solve this problem. Please, I request you that help me to solve this problem.




      Here is the code of the function of the controller which set the $rootScope variable




      $scope.getUserDetails = function () {

      appService.getDetails('/user/getDetails').then(function (response) {
      $scope.current_status = response.current_status;
      $rootScope.current_status = $scope.current_status;
      }
      }



      And here is the code of run method




      if (transition.to().name == "user.stateName"){
      console.log($rootScope.current_status);
      $state.go('store.' + $rootScope.current_status);
      }



      Output of the $rootScope.current_status in console is




      undefined









      share|improve this question














      In my project when user is coming from any url then first i check in the controller that what was the last status of the user if they coming on the page which was not accessible yet then i am set a $rootScope variable to the current status of the user and use this variable in app's run method and send them to state according to there current status in $rootScope which I set in controller, but my issue is that the function which is getting the current status from the nodejs's api is calling after the checking apply in the run method, so the result is in the run method current status in showing undefined because the controller function is running after the code of run method. I not able to understand how to solve this problem. Please, I request you that help me to solve this problem.




      Here is the code of the function of the controller which set the $rootScope variable




      $scope.getUserDetails = function () {

      appService.getDetails('/user/getDetails').then(function (response) {
      $scope.current_status = response.current_status;
      $rootScope.current_status = $scope.current_status;
      }
      }



      And here is the code of run method




      if (transition.to().name == "user.stateName"){
      console.log($rootScope.current_status);
      $state.go('store.' + $rootScope.current_status);
      }



      Output of the $rootScope.current_status in console is




      undefined






      angularjs node.js angular-ui-router






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 3 at 7:00









      user979879user979879

      378




      378
























          2 Answers
          2






          active

          oldest

          votes


















          0














          The run function will execute prior to instantiation of the controller. That's why trying to get the variable in run will yield undefined.



          You could instead, use redirectTo in the state configuration to redirect to a particular state based on some logic.



          .state('user.stateName', {
          ...
          redirectTo: function(transition) {
          var service = transition.injector().get('appService')

          return service.getDetails('/user/getDetails')
          .then(function(response) {
          return response.current_status;
          });
          }
          })





          share|improve this answer
























          • your solution is didn't work for me, but thank you for your efforts.

            – user979879
            Jan 4 at 9:05



















          0














          I solved my problem finally after doing a lot of searches, when I inject the appService and call the appService method in the run method then my problem is solved.






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54017682%2fhow-to-access-variable-of-controller-in-the-run-method%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            The run function will execute prior to instantiation of the controller. That's why trying to get the variable in run will yield undefined.



            You could instead, use redirectTo in the state configuration to redirect to a particular state based on some logic.



            .state('user.stateName', {
            ...
            redirectTo: function(transition) {
            var service = transition.injector().get('appService')

            return service.getDetails('/user/getDetails')
            .then(function(response) {
            return response.current_status;
            });
            }
            })





            share|improve this answer
























            • your solution is didn't work for me, but thank you for your efforts.

              – user979879
              Jan 4 at 9:05
















            0














            The run function will execute prior to instantiation of the controller. That's why trying to get the variable in run will yield undefined.



            You could instead, use redirectTo in the state configuration to redirect to a particular state based on some logic.



            .state('user.stateName', {
            ...
            redirectTo: function(transition) {
            var service = transition.injector().get('appService')

            return service.getDetails('/user/getDetails')
            .then(function(response) {
            return response.current_status;
            });
            }
            })





            share|improve this answer
























            • your solution is didn't work for me, but thank you for your efforts.

              – user979879
              Jan 4 at 9:05














            0












            0








            0







            The run function will execute prior to instantiation of the controller. That's why trying to get the variable in run will yield undefined.



            You could instead, use redirectTo in the state configuration to redirect to a particular state based on some logic.



            .state('user.stateName', {
            ...
            redirectTo: function(transition) {
            var service = transition.injector().get('appService')

            return service.getDetails('/user/getDetails')
            .then(function(response) {
            return response.current_status;
            });
            }
            })





            share|improve this answer













            The run function will execute prior to instantiation of the controller. That's why trying to get the variable in run will yield undefined.



            You could instead, use redirectTo in the state configuration to redirect to a particular state based on some logic.



            .state('user.stateName', {
            ...
            redirectTo: function(transition) {
            var service = transition.injector().get('appService')

            return service.getDetails('/user/getDetails')
            .then(function(response) {
            return response.current_status;
            });
            }
            })






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 3 at 7:10









            31piy31piy

            17.3k52443




            17.3k52443













            • your solution is didn't work for me, but thank you for your efforts.

              – user979879
              Jan 4 at 9:05



















            • your solution is didn't work for me, but thank you for your efforts.

              – user979879
              Jan 4 at 9:05

















            your solution is didn't work for me, but thank you for your efforts.

            – user979879
            Jan 4 at 9:05





            your solution is didn't work for me, but thank you for your efforts.

            – user979879
            Jan 4 at 9:05













            0














            I solved my problem finally after doing a lot of searches, when I inject the appService and call the appService method in the run method then my problem is solved.






            share|improve this answer




























              0














              I solved my problem finally after doing a lot of searches, when I inject the appService and call the appService method in the run method then my problem is solved.






              share|improve this answer


























                0












                0








                0







                I solved my problem finally after doing a lot of searches, when I inject the appService and call the appService method in the run method then my problem is solved.






                share|improve this answer













                I solved my problem finally after doing a lot of searches, when I inject the appService and call the appService method in the run method then my problem is solved.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 5 at 6:54









                user979879user979879

                378




                378






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54017682%2fhow-to-access-variable-of-controller-in-the-run-method%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Monofisismo

                    Angular Downloading a file using contenturl with Basic Authentication

                    Olmecas