我已经完成了所有可能的阅读,但是我缺少将 AngularJS 与 Django Rest 框架一起使用的东西。为了解决“斜线”问题,我使用了restangular。我的模块的相关部分:
var demoApp = angular.module('demoApp', ['ngResource', 'djangoRESTResources', 'restangular']);
demoApp.config(function($httpProvider){
delete $httpProvider.defaults.headers.common['X-Requested-With'];
});
demoApp.factory('CbgenRestangular', function(Restangular) {
return Restangular.withConfig(function(RestangularConfigurer) {
RestangularConfigurer.setBaseUrl('http://localhost:1234');
});
});
demoApp.controller('SimpleController', function($scope, simpleFactory, CbgenRestangular, $location){
var lCems = CbgenRestangular.all("cemeteries/");
$scope.cemeteries = lCems.getList().then(function(cemeteries) {
console.log(cemeteries[0]);
}, function(response) {
console.log(response);
});
TcpMon 似乎显示了正确的响应:
HTTP/1.0 200 OK
Date: Wed, 14 Aug 2013 18:47:04 GMT
Server: WSGIServer/0.1 Python/2.7.5
Vary: Accept, Cookie
Content-Type: application/json; charset=utf-8
Allow: GET, POST, HEAD, OPTIONS
[{"id": 2, "name": "Trinity Anglican New", "latitude": "N 46 14.041", "longitude": "W 060 13.170", "url": "http://127.0.0.1:1234/cemeteries/2/", "api_url": "#/cemeteries/2"}, {"id": 3, "name": "St. John's Anglican New", "latitude": "N 45 30.697", "longitude": "W 061 01.125", "url": "http://127.0.0.1:1234/cemeteries/3/", "api_url": "#/cemeteries/3"}, {"id": 4, "name": "Holy Rosary", "latitude": "N 46 03.139", "longitude": "W 060 48.479", "url": "http://127.0.0.1:1234/cemeteries/4/", "api_url": "#/cemeteries/4"}]
但是我如何访问它?在我看来,这段代码不起作用:
<ul>
<li data-ng-repeat="cem in cemeteries | filter:filter.name">{{ cem.name }}</li>
</ul>
我需要手动解析json吗?如果是,如何?提前感谢您的帮助。
问候,菲尔