我正在尝试使用服务和控制器从angularjs访问django数据,但我似乎遇到了一些麻烦 这是我到目前为止所得到的
var Events = angular.module("Events",["ngCookies"],function($interpolateProvider){
$interpolateProvider.startSymbol("((");
$interpolateProvider.endSymbol("))");
});
Events.run(function ($http, $cookies) {
$http.defaults.headers.common['X-CSRFToken'] = $cookies['csrftoken'];
});
Events.config(function ($routeProvider) {
$routeProvider
.when("/", {
templateUrl:"static/js/myapp/views/main.html",
controller:"MainController",
resolve:{
events:function (EventService) {
return EventService.list();
}
}
})
});
Events.controller('MainController', function ($scope, events) {
$scope.events = events.data;
}
Events.factory('EventService', function ($http, $q) {
var api_url = "/events/";
return {
get: function (event_id) {
var url = api_url + event_id + "/";
var defer = $q.defer();
$http({method: 'GET', url: url}).
success(function (data) {
defer.resolve(data);
})
.error(function (data, status) {
defer.reject(status);
});
return defer.promise;
},
list: function () {
var defer = $q.defer();
$http({method: 'GET', url: api_url}).
success(function (data) {
defer.resolve(data);
}).error(function (data, status) {
defer.reject(status);
});
return defer.promise;
}
}
});
请帮助.....我是新来的。 任何帮助将不胜感激:
修改
模型
class event(models.Model):
title=models.CharField(max_length=200)
date =models.DateTimeField(auto_now_add=True)
description=models.CharField(max_length=200)
type=models.CharField(max_length=200)
我已经通过管理员插入了数据,所以它是响应应用程序,
main.html中
<table border="1px black" >
<tr>
<th>Title</th>
<th>Date</th>
</tr>
<div ng-repeat="event in events">
<tr >
<td>
<a href="((event.event_url))">
((event.title))
</a>
</td>
<td>
((event.description))
</td>
</tr>
</div>
</table>
这是我使用$ routeProvider调用的视图