Angular $http 服务不加载 json 文件
Angular $http service does not load json file
你好角度专家!
我将这个自定义指令用于表(实现),但是当我尝试使用$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;
});
并得到了相同的行为。
提前谢谢你!
我相信
你用错了$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
魔法应该完成剩下的:)
我只是按照文档中提到的方式做这样的事情,它起作用了:
$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', 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.
});
});
<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>
或者您可能更喜欢此演示。
相关文章:
- AngularJS加载JSON数据,然后从中解析/加载HTML
- 在Javascript中加载JSON文件
- 如何在dojo应用程序构建概要文件中加载json文件
- 加载json时出现Ember.js和Ember数据错误
- Webgl 加载 json 模型问题
- 在AngularJS应用程序中加载JSON(加载谷歌电子表格)
- Angular $http 服务不加载 json 文件
- 在 JavaScript 中读取/加载 JSON,其中 'id' = 'x'
- 如何加载JSON数组以使用Angular-Translate翻译AngularJS网页
- 使用 jQuery 加载 JSON,然后将其显示给查看器
- D3.js可以't加载json文件
- 如何使用Angular$http.jsonp从S3加载JSON文件
- 从静态文件加载json时出错
- 如何在Fullcalendar中重新加载JSON数据
- jQuery.Dunatree.如何使用AJAX加载JSON树
- Extjs树面板未加载json数据
- AJAX加载JSON数据并将其存储在angularjs服务中
- 使用jquery加载json嵌套对象
- 如何在CasperJS脚本中通过get-int变量加载JSON
- 在我的firefox插件中加载json文件