Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9000347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:18:01+00:00 2026-06-16T00:18:01+00:00

How can I use angular-resources.js to read in a JSON file through a service?

  • 0

How can I use angular-resources.js to read in a JSON file through a service?

I am working on a very basic Angular app for testing purposes and am just trying to read in data from JSON file right now. I am placing this code in a service so I can more easily swap it out when we move a server based data store.

My App and App.controller declaration are as follows:

'use strict';

// create module for custom directives
var App = angular.module('App', ['jsonService']);

// controller business logic
App.controller('AppCtrl', function AppCtrl($scope, JsonService) {
    console.log("marker 1");

    if (!$scope.jsonData) {
        console.log("marker 2");
        JsonService.getData(function (d) {
            console.log(d);
            $scope.jsonData = d;
            $scope.records = d.length;
        });
    } else {
        console.log("I have data already... " + $scope.jsonData);
    }

    console.log($scope.jsonData);
});

My JsonService is defined as the follow, at the moment:

'use strict';

angular.module('jsonService', ['ngResource'])
.factory('JsonService', function($resource, $filter) {
    // define the remote service using Angular's $resource module
    var service = $resource('/data/ProcessModeling-Resources.json', {});

    var JsonService = {
        // calls $resource.query() to retrieve the remote data.
        getData : function getData(callback) {
            console.log("marker 3");
            service.query(function (data) {
                console.log("marker 4");
            });
        }
    };

    return JsonService;
});

The console output I am getting follows:

marker 1 app.js:8
marker 2 app.js:11
marker 3 services.js:13
undefined app.js:21
TypeError: Object #<Resource> has no method 'push'
    at copy (http://127.0.0.1:8000/lib/angular.js:556:21)
    at new Resource (http://127.0.0.1:8000/lib/angular-resource.js:330:9)
    at http://127.0.0.1:8000/lib/angular-resource.js:386:32
    at forEach (http://127.0.0.1:8000/lib/angular.js:117:20)
    at http://127.0.0.1:8000/lib/angular-resource.js:385:19
    at wrappedCallback (http://127.0.0.1:8000/lib/angular.js:6650:59)
    at http://127.0.0.1:8000/lib/angular.js:6687:26
    at Object.Scope.$eval (http://127.0.0.1:8000/lib/angular.js:7840:28)
    at Object.Scope.$digest (http://127.0.0.1:8000/lib/angular.js:7707:25)
    at Object.Scope.$apply (http://127.0.0.1:8000/lib/angular.js:7926:24) angular.js:5582

I’m receiving my error when I attempt to call my service.query(function (data) { }, which (if I’m understanding correctly) should be pulling my JSON file in.

I’ve been using AngularJS Cats App as an example for pulling data.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:18:02+00:00Added an answer on June 16, 2026 at 12:18 am

    I’d follow @pkozlowski’s advice and make sure the response is an array. Anyway, here’s an example that loads data from a JSON file similar to what you describe in your comments. It uses ngResource and can help you put things together: http://plnkr.co/edit/Ofq7Md8udEnIhAPF1NgL?p=preview

    The service

    angular.module('jsonService', ['ngResource'])
    .factory('JsonService', function($resource) {
      return $resource('cats.json',{ }, {
        getData: {method:'GET', isArray: false}
      });
    });
    

    Notice that isArray is set to false.

    Your app and controller

    var app = angular.module('app', ['jsonService']);
    
    app.controller('ctrl', function($scope, JsonService){
      JsonService.getData(function(data){
        $scope.name = data.name;
        $scope.children = data.children;
      });
    });
    

    getData is actually not needed since the Resource class gives you some useful convenience methods such a get, you can just do this

    angular.module('jsonService', ['ngResource'])
    .factory('JsonService', function($resource) {
      return $resource('cats.json');
    });
    
    app.controller('ctrl', function($scope, JsonService){
      JsonService.get(function(data){
        $scope.name = data.name;
        $scope.children = data.children;
      });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can't use angular $http service for my services. I have to use YQL
I can use this maven plugin maven-jaxb-plugin to generate Java Classes from XSD file.
I use Angular $resource for REST service. Due to the quirk in my get
I can use File('foo.bar').abspath to get the location of a file, but if I've
I have a very strange problem with an angular app we are building. Whenever
When I use yeoman init angular:all to bootstrap my app, yeoman uses the name
I have a service: (angular .module('app.services', ['ngResource']) .factory('MyService', [ /******/ '$resource', function ($resource) {
I can use the following to get the contents of a folder with no
Users can use their Google, Facebook or Twitter account to login to a site
I can use .button() to create beautiful submit buttons, but the rest of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.