0

Script.js

        var app=angular.module("myapp",["panelModule"]);
        var store=this;
        store.products=[];

        //following code yield proper output
        // app.controller("StoreController",function () {
        //  this.products=data; //data is a array of object defined in same file
        // });

        app.controller("StoreController",['$http',function($http){
            return $http.get('js/data.json').then(onDataReceive);
        }]);

        var onDataReceive=function(response) {
            store.products.push(...response.data);
            console.log(store.products); //this shows [object, object] in browser console

        };

I m basically iterating over the array of object with ng-repeat in my view( index.html) But it is not getting update when i used $http service.However static data is getting displayed properly. I m going thru some online AngularJs tutorial. I m new to AngularJs. Please point me out what i m doing wrong here? Thanks

4
  • What is the browser you are using? Try using Array.prototype.push.apply(store.products, response.data) instead of store.products.push(...response.data); Commented Feb 28, 2017 at 13:40
  • Why are you using your store property and onDataReceive function outside of the controllers? Commented Feb 28, 2017 at 13:43
  • That code is really weird, why is store outside Angular? I really suggest you going through the Angular tutorial so that you acquire the basics. Commented Feb 28, 2017 at 13:43
  • @Paraíso I m using Chrome Commented Feb 28, 2017 at 13:44

2 Answers 2

2

Try this :

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


// do the ajax call
getData().then(function(data) {
    // stuff is now in our scope, I can alert it
    store.products = $scope.data;
    console.log(store.products);
});
Sign up to request clarification or add additional context in comments.

Comments

1
var app = angular.module("myapp", ["panelModule"]);
app.controller("StoreController", ['$http', function($http) {
  var store = this;
  store.products = [];
  gettingAPIData();
  function gettingAPIData() {
    $http.get('js/data.json').then(function(response) {
        store.products. = response.data;
        onDataReceive();
    });
  }
  function onDataReceive() {
    console.log(store.products);
  };
}]);

This is the way to do it, you first create a method and do the http stuff in it. Call another method in the success response and print it out.

2 Comments

i got one doubt ... the function passed as success promise is anonymous right .. also the onDataReceive should be response. Please correct me if i m wrong
yes that's a typo $http.get('js/data.json').then(function(response) { here i'm not calling onDateReceive function, it's just reponse. edited & corrected.. Please try

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.