0

I am trying to read a JSON data from a file using angular factory and service. But Angular can't see my defined service. Here is my factory:

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

And my controller:

//app.js
'use strict';

angular.module('myApp', [
'ngRoute',
'myApp.view1',
'myApp.view2',
'myApp.version',
'jsonService'
]).config(['$routeProvider', function($routeProvider) {
   $routeProvider.otherwise({redirectTo: '/view1'});
}]).
controller('mainController', ['JsonService', function($scope, JsonService){
    JsonService.getData(function(data) {
      console.log("Test");
      $scope.length = data.length;
  })}]);

And I get:

"Error: JsonService is undefined"

2 Answers 2

3

You're injecting 'JsonService' as the first parameter of your function, but you are using the JsonService as the 2nd parameter of your function.

Should be ['JsonService', '$scope', function($scope, JsonService){ or if $scope not needed then ['JsonService', function(JsonService){

Sign up to request clarification or add additional context in comments.

Comments

1

This line in app.js should look like

controller('mainController', ['$scope','JsonService', function($scope, JsonService){

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.