How to assign a $http.get response to a variable AngularJS

D_Breedt

There have been many similar questions on stack overflow, but using the tips from this have not helped.

I am learning angular.js and am experimenting with $http.get on an API, but am experiencing issues while assigning the response to a variable.

the below get request is run on load. (this is my app.js file)

    var app = angular.module('store',[]);
    app.controller("StoreController", function($scope,$http){
        var request = $http.get('/test').then(function (response) {
            $scope.data = response;
            return response; 
        });
        request.then(function (data) {
            this.products = $scope.data
        });
    });

The below is the response from the request (I can see this in the console view of the browser, but the data is not being displayed.)

    [
        {
        name: 'Dowayne',
        surname: 'Breedt',
        catchphrase: 'wallawallabingbangamIright?',
        reviews: [
                 {
                 stars: 5,
                 body: 'i love stuff',
                 soldOut: false,
                 },
                 {
                 stars: 4,
                 body: 'meh',
                 soldOut: true,
                 },
                 {
                 stars: 3,
                 body: 'shittake mushrooms',
                 soldOut: false,
                 },
                 ]
       }
    ];

This is all junk data (just for practice) but I can't for the life of me understand why it won't display on the HTML page, if I assign the above snippet directly to this.products, then it works perfectly/

What am I missing?

Thank you.

Sandeep

when use Promises as then use .data property to getting data.

var app = angular.module('store',[]);
 app.controller("StoreController", function($scope,$http){
    var request = $http.get('/test').then(function (response) {
        $scope.data = response.data;
        return response.data; 
    });
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

AngularJS: assign $http.get data to variable

From Dev

AngularJS: assign $http.get data to variable

From Dev

How to wait till the response comes from the $http request and assign value in $scope variable in angularjs?

From Dev

How to get the ajax response from success and assign it in a variable using jQuery?

From Dev

How to get the ajax response from success and assign it in a variable using jQuery?

From Dev

Angularjs :- how to get funtion's response using $http.get()?

From Dev

How to use external variable inside promise response of $http.get

From Dev

How to use external variable inside promise response of $http.get

From Dev

How to assign response to variable in angular 4?

From Dev

AngularJs assign http response object to scope from html

From Dev

Get $http response error status on AngularJS

From Dev

How to increase variable which I get in response and assign new value in next request JMETER

From Dev

How to get the response content of an HTTP 404 response

From Dev

How to assign value to variable outside the function in $on AngularJS?

From Dev

how get response http header?

From Dev

how get response http header?

From Dev

how to get response of http request

From Dev

AngularJS - How to pass additional params in $http response

From Dev

How to return AngularJS Function's $http response?

From Dev

angular2 How to store response header data from http.get in extra variable?

From Dev

How to assign value from url xml response to a variable in visual studio?

From Dev

How to get Response after POST in AngularJS?

From Dev

how to get headers from response in angularjs

From Dev

AngularJS $http response header

From Dev

AngularJS $http response header

From Dev

AngularJS injection of http response

From Dev

Initialize AngularJS Constant using $http.get call response

From Dev

AngularJS $http get data object displays status code instead of response

From Dev

Typescript angularjs $http get response type (avoid use of <any>)

Related Related

  1. 1

    AngularJS: assign $http.get data to variable

  2. 2

    AngularJS: assign $http.get data to variable

  3. 3

    How to wait till the response comes from the $http request and assign value in $scope variable in angularjs?

  4. 4

    How to get the ajax response from success and assign it in a variable using jQuery?

  5. 5

    How to get the ajax response from success and assign it in a variable using jQuery?

  6. 6

    Angularjs :- how to get funtion's response using $http.get()?

  7. 7

    How to use external variable inside promise response of $http.get

  8. 8

    How to use external variable inside promise response of $http.get

  9. 9

    How to assign response to variable in angular 4?

  10. 10

    AngularJs assign http response object to scope from html

  11. 11

    Get $http response error status on AngularJS

  12. 12

    How to increase variable which I get in response and assign new value in next request JMETER

  13. 13

    How to get the response content of an HTTP 404 response

  14. 14

    How to assign value to variable outside the function in $on AngularJS?

  15. 15

    how get response http header?

  16. 16

    how get response http header?

  17. 17

    how to get response of http request

  18. 18

    AngularJS - How to pass additional params in $http response

  19. 19

    How to return AngularJS Function's $http response?

  20. 20

    angular2 How to store response header data from http.get in extra variable?

  21. 21

    How to assign value from url xml response to a variable in visual studio?

  22. 22

    How to get Response after POST in AngularJS?

  23. 23

    how to get headers from response in angularjs

  24. 24

    AngularJS $http response header

  25. 25

    AngularJS $http response header

  26. 26

    AngularJS injection of http response

  27. 27

    Initialize AngularJS Constant using $http.get call response

  28. 28

    AngularJS $http get data object displays status code instead of response

  29. 29

    Typescript angularjs $http get response type (avoid use of <any>)

HotTag

Archive