Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Angular$http服务不加载json文件_Javascript_Json_Angularjs - Fatal编程技术网

Javascript Angular$http服务不加载json文件

Javascript Angular$http服务不加载json文件,javascript,json,angularjs,Javascript,Json,Angularjs,各位专家好 我对一个表()使用了这个自定义指令,但当我尝试使用$http服务从文件加载json数组时,json并没有加载到$scope.items中,我是angular和相当高级的javascript的初学者,因此我需要您的帮助 控制器初始化 fessmodule.controller('ptiListController', function($http, $scope, $filter) { $http服务调用 $http.get('data/ptis/ptis.json'

各位专家好

我对一个表()使用了这个自定义指令,但当我尝试使用$http服务从文件加载json数组时,json并没有加载到$scope.items中,我是angular和相当高级的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">
我改为:

    $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;
    });
也有同样的行为


提前谢谢你

我相信您使用的是
$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={};// 我只是做了一些类似的事情,正如在中提到的那样,它起作用了:

      $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.
      });
以下是示例:

angular.module('myApp',[])
.controller('JustCtrl',函数($scope,$http){
$scope.ptis=[];
//简单的GET请求示例:
$http.get('https://gist.githubusercontent.com/idhamperdameian/239cc5a4dbba4488575d/raw/0a2ea4c6c120c9a8f02c85afcf7a31941ef74d3a/ptis.json').
成功(函数(数据、状态、标题、配置){
//将异步调用此回调
//当响应可用时
$scope.ptis=数据;
}).
错误(函数(数据、状态、标题、配置){
//发生错误时异步调用
//或服务器返回带有错误状态的响应。
});
});

{{p.name}}、{p.description}}等…

您也可以发布您的目录结构吗?回答可以吗?JSON有效吗?检查jsonlint.com我看不到完整的代码,是否有可能在response.data成功分配给$scope.items之前尝试使用了$scope.items?@ta run-是,json是valid@aurelius在这种情况下,为什么要使用
.then()
?您可以直接尝试
.success()
和/或
.error()
。@ta运行您可以看到上面,我已经编辑了这个问题,并添加了我尝试以您建议的方式调用$http,得到了相同的行为…相同的行为:TypeError:无法读取作用域中未定义的属性“length”。$Scope.grouptopage()在Scope.$Scope.search()在new)在invoke()在Object.instantiate…我已经更新了我的答案,请注意,您不能使用还不存在的对象。这应该行。好的,我知道你做了什么。。。当dom加载到浏览器中时,我得到items={}它可以工作,只需将它包装到控制器回调中,就可以开始了:)
      $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.
      });