我是angularjs的新手,由于某种原因,当json来自joomla组件时,angular js不会在页面上加载任何数据。
Angularjs在我从位于站点根目录中的getcustomers.php文件中获取数据时有效,但在json数据来自joomla组件时无效。(http://localhost/testsite/index.php?option=com_helloworld&view=helloworld&format=raw)。
我可以看到json正在加载(当页面加载时,直接点击getcustomers.php和joomla组件的url也可以在chrome开发工具中看到)。
我可以看到,这两个json响应中唯一的区别是双括号[{"Id":87,"EANHotelID": }]
(工作json示例),而不工作的json示例是[[{"Id":87,"EANHotelID":}]]
。
Angularjs控制器如下:(工作和不工作之间的唯一区别是下面的HTTP.GET:
var app = angular.module('myApp', ['ui.bootstrap']);
app.filter('startFrom', function() {
return function(input, start) {
if(input) {
start = +start; //parse to int
return input.slice(start);
}
return [];
}
});
app.controller('customersCrtl', function ($scope, $http, $timeout) {
$http.get('testsite/index.php?option=com_helloworld&view=helloworld&format=raw').success(function(data){
$scope.list = data;
$scope.currentPage = 1; //current page
$scope.entryLimit = 10; //max no of items to display in a page
$scope.filteredItems = $scope.list.length; //Initially for no filter
$scope.totalItems = $scope.list.length;
});
$scope.setPage = function(pageNo) {
$scope.currentPage = pageNo;
};
$scope.filter = function() {
$timeout(function() {
$scope.filteredItems = $scope.filtered.length;
}, 10);
};
$scope.sort_by = function(predicate) {
$scope.predicate = predicate;
$scope.reverse = !$scope.reverse;
};
});
希望你能帮忙。
两件事:
-
正在提取的JSON无效。如果不存在值,则应为"EANHotelID"属性指定一个值,例如null。它看起来是这样的:
[{"Id":87,"EANHotelID":null}]
-
第二个不起作用的响应是一个数组,里面有一个数组。要像现在这样访问数据,你必须将$http.get成功函数中的第一行从:
$scope.list=数据;
至
$scope.list=data[0];
最好在没有额外数组包装的情况下发送响应,因为您不需要它。