为什么我不能使用$ resource来读取json数据

时间:2013-07-31 08:16:08

标签: angularjs

我想用$ resource

读取json数据

但是这里有一个错误“JsonService未定义”

这是我的代码

<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;
});

我很困惑,请帮忙

1 个答案:

答案 0 :(得分:3)

您没有在控制器中注入JsonService,您应该修改代码,如下所示。

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

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