Angular $ http服务不加载json文件

时间:2015-03-16 09:07:25

标签: javascript json angularjs

你好角度专家!

我将这个自定义指令用于表(implementation)但是当我尝试使用$ http服务加载文件中的json数组时,json没有加载到$ scope.items中,我是初学者角度和相当先进的JavaScript因此我需要你的帮助。

控制器初始化

    fessmodule.controller('ptiListController', function($http, $scope, $filter) {

$ http服务电话

    $http.get('data/ptis/ptis.json').then(function(response) {
            $scope.items = response.data;
        }
    );

浏览器控制台错误

TypeError: Cannot read property 'length' of undefined
at Scope.$scope.groupToPages (http://localhost:8000/app/modules/app/phone/scripts/pti-list-controller.js:75:49)
at Scope.$scope.search (http://localhost:8000/app/modules/app/phone/scripts/pti-list-controller.js:68:16)
at new <anonymous> (http://localhost:8000/app/modules/app/phone/scripts/pti-list-controller.js:117:12)
at invoke (http://localhost:8000/app/lib/js/angular.js:4185:17)
at Object.instantiate (http://localhost:8000/app/lib/js/angular.js:4193:27)
at http://localhost:8000/app/lib/js/angular.js:8462:28
at link (http://localhost:8000/app/lib/js/angular-route.js:975:26)
at invokeLinkFn (http://localhost:8000/app/lib/js/angular.js:8219:9)
at nodeLinkFn (http://localhost:8000/app/lib/js/angular.js:7729:11)
at compositeLinkFn (http://localhost:8000/app/lib/js/angular.js:7078:13) <div ng-view="" class="ng-scope">

所以我从小提琴中改变的是:

而不是:

$scope.items = [
    {"id":1,"name":"name 1","description":"description 1","field3":"field3 1","field4":"field4 1","field5 ":"field5 1"}, 
    {"id":2,"name":"name 2","description":"description 1","field3":"field3 2","field4":"field4 2","field5 ":"field5 2"}, 
    {"id":3,"name":"name 3","description":"description 1","field3":"field3 3","field4":"field4 3","field5 ":"field5 3"}
];

我已改为:

    $http.get('data/ptis/ptis.json').then(function(response) {
            $scope.items = response.data;
        }
    );

而且,我已尝试将服务调用用作:

    $http.get('data/ptis/ptis.json').success(function(data) {
        $scope.items = data;
    });

并得到了相同的行为。

提前谢谢!

2 个答案:

答案 0 :(得分:1)

我相信您使用的是$http.get错误。试试$http.JSONP这种模式:

$scope.items = {}; // <-- initialize empty object

$http.jsonp('/someJSONUrl').
  success(function(data) {
    // this callback will be called asynchronously
    // when the response is available
    $scope.items = data; // <-- fill object with data
  });

在保存某些数据之前,您无法使用$scope.items。这就是为什么你必须首先初始化它,因为空对象/数组然后用数据填充它,angular魔法应该完成其余的工作:)

答案 1 :(得分:1)

我只是在document中提到这样的事情并且有效:

      $http.get('someUrl').
      success(function(data, status, headers, config) {
        // this callback will be called asynchronously
        // when the response is available
      }).
      error(function(data, status, headers, config) {
        // called asynchronously if an error occurs
        // or server returns response with an error status.
      });

这里是样本:

&#13;
&#13;
angular.module('myApp', [])
  .controller('JustCtrl', function($scope, $http) {
    $scope.ptis = [];
    // Simple GET request example :
    $http.get('https://gist.githubusercontent.com/idhamperdameian/239cc5a4dbba4488575d/raw/0a2ea4c6c120c9a8f02c85afcf7a31941ef74d3a/ptis.json').
    success(function(data, status, headers, config) {
      // this callback will be called asynchronously
      // when the response is available
      $scope.ptis = data;
    }).
    error(function(data, status, headers, config) {
      // called asynchronously if an error occurs
      // or server returns response with an error status.
    });
  });
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="JustCtrl">
  <span ng-repeat="p in ptis">
        {{p.name}}, {{p.description}}, etc...<br>
    </span>
</div>
&#13;
&#13;
&#13;

或者你可能更喜欢this demo