0

Created factory that will return json data and calling it from controller, but it is coming empty. don't know where I made mistake. no console error and in network json is also loading.

    'use strict';
var app = angular.module('angularJson', ['ngResource']);

    app.factory('loadJsonService', ['$resource', function ($resource) {
        return {
            getData: function () {
                return $resource('json/productDetails.json');
            }
        };
    }])

  app.controller('angularJsonCtrl',['$scope', 'loadJsonService',function($scope, loadJsonService){

    $scope.loadProducts = function(noOfData){
        $scope.productDetails = [];
        $scope.productDetails = loadJsonService.getData().query();
    }

  }]);
Pankaj Parkar
  • 134,766
  • 23
  • 234
  • 299
Mannu
  • 71
  • 1
  • 9

3 Answers3

3

You have to put wait till request gets completed & there after you need to use .$promise.then over $resource.query function call. So that you could put function which will get call on success of API call.

loadJsonService.getData().query().$promise.then(function(res){ //success function
   $scope.productDetails = res;
}, function(error){ //error function
   console.log(error);
})
Pankaj Parkar
  • 134,766
  • 23
  • 234
  • 299
0

You must in getData function set $http request like this

$http.jsonp("json/productDetails.json")
        .success(function(response) {
          callback(response);
        });
Oleg Utkin
  • 122
  • 1
  • 4
0

Try built in query method. If you want to write your own get method do the following,

{
   method: 'GET',
   params: {
   key: value
},
isArray: true,
cache: true // if you want to cache
}
Vinay
  • 548
  • 2
  • 12