0
.config([
    '$provide',
    function ($provide) {
        'use strict';



        var initInjector = angular.injector(['ng']);

        var apiUrl;

        initInjector.get('$http').get('customer/api/')

       .then(function (response) {
            console.log("dddd" + response.data.url);

            apiUrl = response.data.url;

          });

       console.log(apiUrl);

//This print as Undefined

        $provide.constant('GLOBAL', global);
    }
]);

when i print the apiUrl value tetrived from http get service print as undefined.

can some one hep me to get the value of response.data.url to outer scope.

Edit:

    var apiUrl;

    var initInjector = angular.injector(['ng']);

    var self = this;

    initInjector.get('$http').get('customer/api/')

    .then(function(response) {

        console.log("ddddd" + response.data.url); //print value
        self.apiUrl = response.data.url;

        console.log("selff" +self.apiUrl); //print value
    });

    console.log("outerr" +self.apiUrl); // print undefined
Niranga Sandaruwan
  • 691
  • 2
  • 19
  • 39
  • Then don't do your `console.log` call until you know you have the response. You have the response when the REST call finishes, so do your printing/updating of the UI there. You can also add error handling and respond appropriately as well. – Tim Biegeleisen Dec 04 '17 at 11:24
  • i want to get apiUrl value for passing to service in angularjs – Niranga Sandaruwan Dec 04 '17 at 11:26
  • `$http.get` is an asynchronous call, use `console.log()` inside of `then()`. Work with promises and not variables (return `apiUrl`) – Aleksey Solovey Dec 04 '17 at 11:27
  • You can move your code into service, where you want to pass apiUrl. – Kaustubh Khare Dec 04 '17 at 11:28
  • It would help us to know where the data is needed. This pattern could be helpful: https://stackoverflow.com/questions/15937267/inject-service-in-app-config – Brian Dec 04 '17 at 11:44

2 Answers2

0

Promises can't be used like synchronous code because they return a promise immediately. You should wait for the promise to complete before setting your global values.

Instead of doing

a = {}
a.key1 = Promise().then(()=>{
  return 1
})
a.key2 = Promise().then(()=>{
  return 2
})

Do something like this instead (with bluebird):

Promise.all([
    Promise().then(()=>{
      return 1
    }),
    Promise().then(()=>{
      return 2        
    }),
]).then(result => {
  a.key1 = result[0]
  a.key2 = result[1]
})

Alternatively you can look into the async/await syntax es7 provides

danieltan95
  • 810
  • 7
  • 14
  • i dont use es7.this is legacy code base.can you modify my code accordingly. – Niranga Sandaruwan Dec 04 '17 at 11:34
  • https://docs.angularjs.org/api/auto/service/$provide#constant I think you might want to look into using some other methods for angular specifically – danieltan95 Dec 04 '17 at 11:43
  • var a = {}; var initInjector = angular.injector(['ng']); initInjector.get('$http').get('api/customer') .then(function(response) { console.log("ddddd" + response.data.url); a.key1 = response.data.url; }); console.log(a.key1); still get undefined – Niranga Sandaruwan Dec 05 '17 at 06:17
0

Declare a global variable and then assign value after http call.

var apiUrl ='';
.config([
    '$provide',
    function ($provide) {
        'use strict';
        var initInjector = angular.injector(['ng']);

        var apiUrl;

        initInjector.get('$http').get('customer/api/').then(function (response) {
            console.log("dddd" + response.data.url);

            this.apiUrl = response.data.url;

          });

       console.log(this.apiUrl);
gaurav bankoti
  • 224
  • 1
  • 8