KingStakh
KingStakh

Reputation: 313

Get Json from $http request

Need help to make changes in Angular Factory. Have example working with array JSON data, but need this code work with JSON data from $http request, how do that?

This is example Factory:

    angular.module('starter.services', [])

.factory('catgs', function() {
  // Might use a resource here that returns a JSON array

  // Some fake testing data
  var catgs = [{
    id: 0,
    name: 'CATG1',
    items: [{
      img:'img/G1I1.png',
      name:'category1Item1',
      status:'OPEN'

       }]
  },{
    id: 1,
    name: 'CATG2',
    items: [{
      img:'img/G2I1.png',
      name:'category2Item1',
      status:'OPEN'

       },{
      img:'img/G2I2.png',
      name:'category2Item2',
      status:'CLOSED'

       }]
  }];

  return {
    all: function() {
      return catgs;
    },
    remove: function(catg) {
      catgs.splice(catgs.indexOf(catg), 1);
    },
    get: function(catgId) {
      for (var i = 0; i < catgs.length; i++) {
        if (catgs[i].id === parseInt(catgId)) {
          return catgs[i];
        }
      }
      return null;
    }
  };
});

Need help to replace catgs JSON data to $http request, for example musite.com/jsondata.json

Upvotes: 0

Views: 48

Answers (1)

Nainish Modi
Nainish Modi

Reputation: 488

//try this
$http.get('your api url or .json').then(function(result){
   $scope.jsonData = result.data;
}, function(err) {
   console.log(err);
});

Upvotes: 1

Related Questions