1

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)

1 Answer 1

2

(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"
  }
}
Sign up to request clarification or add additional context in comments.

Comments

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.