Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm an beginner in angularjs and don't have any idea about web services either. My requirement is something like this:

I have to get a JSON object from my url http://mylocalhostname.dev/users/list?city_id=12

I'm absolutely clueless...Please help me out by modifying my code.

Thanks in advance. Here's my JS code:

'use strict';

var services = angular.module('services', ['ngResource']);

services.factory('dataFactory', ['$resource','$http', '$log',
        function ($resource, $http, $log) {
                return {
                    getAllUsers: function(){
                        return  $resource('http://mylocalhostname.dev/users/list',{city_id:12}
                            ,{
                                locate: {method: 'GET', isArray: true, transformResponse: $http.defaults.transformResponse.concat(function(data, headersGetter)
                                  $log.info(data.UsersList[0]);
                                  return data.UsersList[0].userName;
                                })}
                            }
                        );
                    }
                } 
    }]); 

when i test i get this message in my console :

GET http://mylocalhostname.dev/users/list?city_id=12 200 OK 164ms angular.js (ligne 7772) (in color red) (an empty string)

share|improve this question

1 Answer 1

up vote 2 down vote accepted

(I realize this is an ancient question, but still unanswered, and it appeared at the top of a search I just did, so hopefully this can close that particular loop)

Simple example of a controller retrieving data from a simple factory, where the factory is using $resource to access a locally-hosted file and returning contents of file back to the controller.

The factory:

'use strict';
angular.module('myApp')
  .factory('myFactory', ['$resource', function($resource) {
     return function(fileName){
       return $resource(fileName, {});
     };
  }]);

The controller:

'use strict';
var fileToGet = 'some/path/to/file.json';
angular.module('myApp')
    .controller('myController', function($scope, myFactory) {
   var getDefs = new myFactory(fileToGet).get(function(data) {
      $scope.wholeFile = data;
      // $scope.wholeFile should contain the entire JSON-formatted object

      $scope.someSection = data.section;
      // $scope.someSection should contain the "varX" and "varY" items now

      $scope.myStringX = JSON.stringify(data.section.varX);
      // get the stringified value 
   });
});

The JSON-formatted file:

{
  "someVar": "xyz",
  "someArray": [
    {
      "element0A": "foo",
      "element0B": "bar"
    },
    {
      "element1A": "man",
      "element1B": "chu"
    }
  ],
  "section": {
     "varX": 0,
     "varY": "yyyy"
  }
}
share|improve this answer
    
tinks, it works now :) –  Abderr-info Apr 8 '14 at 9:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.