1

I can't pass the content of one variable inside $http.get() to outside of this method... it's always undefined.

I tested with $rootScope, but it didn't work.

controller('myControl', function ($scope, $http) {
    var content;

    $http.get('../Json/data.json').success(function (data, content) {
        content = data;
    }).error(function (data, status, headers, config) {
        $scope.dataJson = "ERROR";
    });

    console.log(content); 
});
Mikko Viitala
  • 8,344
  • 4
  • 37
  • 62
llena8583
  • 21
  • 1
  • 3
  • 1
    `content` is the second argument of your callback and also a global variable, that's not right. – GillesC Jan 29 '15 at 10:12
  • Make sure you use a console.log to test it, rather than evaluating in chrome dev tools, since sometimes says it's undefined when it really isn't. – Wawy Jan 29 '15 at 10:17
  • Don't pass content in your parameters for success function. This is a global variable. – Harsh Jan 29 '15 at 10:19
  • ok, but how I can get the value I get inside of $http, outside of this method?? – llena8583 Jan 29 '15 at 11:23
  • Is not duplicated.. I want to do a async peticion with $http in ANGULAR, like in this way: Promises in AngularJS...http://andyshora.com/promises-angularjs-explained-as-cartoon.html – llena8583 Jan 29 '15 at 13:50

3 Answers3

2

There are two problems here:

  • The content parameter in your success handler is shadowing the content variable in your controller.
  • You are trying to write content to the console before it has a value. This will not work because $http.get() is asynchronous.

To fix these problems:

  • Remove the content parameter. It serves no purpose.
  • Use the content variable inside your success callback.
controller('myControl', function ($scope, $http) {
    var content;

    $http.get('../Json/data.json').success(function (data) {
        content = data;

        console.log(content);
        $scope.dataJson = content;
    }).error(function (data, status, headers, config) {
        $scope.dataJson = "ERROR";
    });
});
JLRishe
  • 99,490
  • 19
  • 131
  • 169
  • I only use this console to watch the value, but the final purpouse is use something like that: $scope.dataJson = this.content; I can't remove the content because I need to get the content outside... – llena8583 Jan 29 '15 at 11:22
  • @llena8583 Ok, if you want to assign it to the scope, then do so (see my edit). I don't see a problem. _"I can't remove the content because I need to get the content outside"_ No, what you were doing was serving no purpose whatsoever. Giving your `success` callback a `content` parameter doesn't accomplish anything. – JLRishe Jan 29 '15 at 11:35
1

First of all, you don't wait for asynchronous $http.get() to finish so console.log() will always print out undefined.

Second, maybe you could think about using then() rather than success().
http://bit.ly/18xIHio

The following should work just fine for you.

/* JS */
app.controller('myControl', function($http) {
    var ctrl = this;

    $http.get('http://www.data.fi/data.json').then(function (response) {
        ctrl.content = response; // use response.data to get the payload
    }).catch(function (error) {
        ctrl.content = error;
    }).finally(function() {
        console.log(ctrl.content); 
    });
});

<!-- HTML -->
<div ng-controller="myControl as ctrl">{{ ctrl.content | json }}</div>
Mikko Viitala
  • 8,344
  • 4
  • 37
  • 62
  • Don't think that `this` will point to what you want in your example here. – Davin Tryon Jan 29 '15 at 10:51
  • Thanks, I was testing your code, but the problem is I need to get the value of the response out of the $http.get, for use like: $scope.dataJson = this.content; I'm using MorrisJs to load wiith this data a Chart, but I try to use inside this scope but didn't work... works outside of this $http this is the reasson because I want to get this value outside. With your code the content is always undefined, I check that directly in the Debugger from Firefox – llena8583 Jan 29 '15 at 11:18
0

Don't pass content in your parameters for success function. This is a global variable. Passing it would create scoping issues. Also use console.log both inside success function and outside it.

Harsh
  • 1,665
  • 1
  • 10
  • 16