user2473037
user2473037

Reputation: 633

Why can't I use $resource to read json data

I want to read json data with $resource

but here has one error "JsonService is not defined"

here is my code

<div ng-controller="Test2Ctrl">
 <accordion close-others="oneAtATime">
<accordion-group heading="Static Header">
  This content is straight in the template.
</accordion-group>
<accordion-group heading="{{group.title}}" ng-repeat="group in groups">
  {{group.content}}
</accordion-group>
<accordion-group heading="Dynamic Body Content">
  <p>The body of the accordion group grows to fit the contents</p>
    <button class="btn btn-small" ng-click="addItem()">Add Item</button>
    <div ng-repeat="item in items">{{item}}</div>
</accordion-group>

'use strict';
 angular.module('jsonService', ['ngResource'])
 .factory('JsonService', function($resource) {
 return $resource('data/data.json'); });



'use strict'; 
 angular.module('elnApp')
.controller('Test2Ctrl', function ($scope) {
  $scope.oneAtATime = true;

JsonService.get(function(data){
  $scope.title = data.title;
  $scope.content = data.content;
});

I am so confused , please help

Upvotes: 0

Views: 186

Answers (1)

Ajay Singh Beniwal
Ajay Singh Beniwal

Reputation: 19037

You are not injecting the JsonService in the controller you should modify your code as below.

angular.module('elnApp')
.controller('Test2Ctrl', function ($scope,JsonService) {
  $scope.oneAtATime = true;

JsonService.get(function(data){
  $scope.title = data.title;
  $scope.content = data.content;
}); 

Upvotes: 3

Related Questions