0

I have below code

$scope.init = function (){        
        console.log($scope.languageFilePath);
        $http.get($scope.languageFilePath) //languageFilePath contain url
       .then(function(res){
            console.log('scueese');          
            $scope.translate = res.data;          
            console.log($scope.translate.SERVICE);
            // $scope.eqipment = $scope.translate['COLOR'];
            //console.log("Data String "+equip);
            //$scope.eqipment="ghsh"
        });    
    };
    $scope.init();

Now what is my issue this method calling properly but before initializing $scope.translate its executing other part of code.

What changes i have to do in the code so first its initialize $scope.translate then only it call some other methods of file .

Note:- $http.get() method calling when this method invoked after that other methods are executing and code inside then executing after those method called.

7
  • 1
    I have 3 questions . This init is for only a part of your app or for only this scope ? are you sure thre is no other function call before ? what is executed before ? Commented Aug 11, 2016 at 12:14
  • @DMCISSOKHO the above code executing but then condition calling after other function called . Commented Aug 11, 2016 at 12:15
  • you mean that a code that is under $scope.init() is invoked before $scope.translate, correct? Commented Aug 11, 2016 at 12:16
  • that's the nature of js.. Commented Aug 11, 2016 at 12:17
  • @AvinashRaj then how to initialize $scope.translate before calling other function because this data used by other function Commented Aug 11, 2016 at 12:20

3 Answers 3

1

If your problem is to execute the code after $scope.init() invocation, you could try to use the http.get promise, like this:

    $scope.init = function (){        
        console.log($scope.languageFilePath);
        return $http.get($scope.languageFilePath) //languageFilePath contain url
          .then(function(res){
            console.log('scueese');          
            $scope.translate = res.data;          
            console.log($scope.translate.SERVICE);
        });    
    };
    $scope.init().then(function(){
      //this code will be executed after the http get...
});

EDIT 1

after receiving some more code from OP, you should modify your .js file as shown here: http://pastebin.com/mFUWSU0N

this way, ALL the code below init() will be executed after the $http.get completion.

Sign up to request clarification or add additional context in comments.

10 Comments

Do you mean i have to give two then ?
Give to who? This way, you are "chaining" the promise of the $http.get ($http uses promises to perform http requests). More simply, with the above code you will execute code after your http.get
i am getting this error angular.js:12330 TypeError: Cannot read property 'then' of undefined
Please provide a plunker of your issue. as you can see from here, there's nothing wrong on promise chain: plnkr.co/edit/A9fq3IAHtC45L7KY2J0k?p=info so maybe the problem it's related to your code.
I dont know how you run my code in plnkr because for me data is loading from rest url. Do you want to check the full file then i can add it there.
|
0

may be you should out the wanted function in your init:

$scope.init = function (){        
        console.log($scope.languageFilePath);
        return $http.get($scope.languageFilePath) //languageFilePath contain url
          .then(function(res){
            console.log('scueese');          
            $scope.translate = res.data;          
            console.log($scope.translate.SERVICE);
            myFunc();
            myFunc2();
        });    
    };

3 Comments

i cannt change code like this some of the function related to ui-grid which i don't have direct control
so the ui-grid is initialized before your init
Yes ui-grid initialized before data initialize in the variable.
0

What you also could do is make it a dependency of your controller. Lets say for instance you are using the routeprovider to navigate to your page you could then do this:

myApp.config(['$routeProvider',function ($routeProvider) {  
    $routeProvider.when('../../goto/yourpage', {
        templateUrl: '/yourpage.html', 
        controller: 'myController',
        resolve: {
            initializeFunction: function($http) {
                // do some logic you want executed before anything else
                 return $http.get("blablabla").$promise;
              });    
            }
        }
    });   
}]);

then in your controller that is active on yourpage.html you can do:

myApp.controller('myController',function ($scope,initializeFunction) {
    $scope.possibleReturnFromTheFunction = initializeFunction; 
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.