我有一个简单的RESTful API使用Django Rest Framework进行设置,我试图使用AngularJS来使用它。我已阅读有关角度控制器和资源,我正在遵循多个教程,但出于某种原因,在我的模板中,我似乎无法正确访问JSON。无法使用REST API
app.js
var blogApp = angular.module('blogApp', ['ngResource']);
blogApp.factory('Post', ['$resource', function($resource) {
return $resource('/api/posts/:slug/', { slug:'@slug'});
}]);
blogApp.controller('postController', ['$scope', 'Post', function($scope, Post) {
$scope.posts = []
Post.query().$promise.then(function(posts) {
$scope.posts = posts;
for (key in posts){
console.log(posts[key]);
}
});
}]);
的index.html **** ****编辑我加了这里的过滤器进行测试,它也做过滤如预期,但仍没有内容显示了<div>
的
<div class="container">
<div class="col-sm-10">
<div ng-controller="postController">
<input type="text" ng-model="textTest">
<div class="well" ng-repeat="(key, post) in posts | filter: textTest">
<h1>{{ post.title }}</h1>
{{ post.content }}
</div>
</div>
</div>
</div>
这是,如果我做/ API /职位得到什么返回/
[
{
"url": "http://localhost:8000/api/posts/i-had-a-api-his-name-was-bingo/",
"id": 0,
"title": "I had a api... his name was Bingo",
"content": "E-I-E-I-O",
"owner": "Heather",
"comments": "http://localhost:8000/api/posts/i-had-a-api-his-name-was-bingo/comments/"
},
{
"url": "http://localhost:8000/api/posts/if-i-could-show-you-the-world/",
"id": 1,
"title": "If I Could Show You The World",
"content": "It would be shining, and shimmering",
"owner": "Heather",
"comments": "http://localhost:8000/api/posts/if-i-could-show-you-the-world/comments/"
},
...
]
在模板中,所有显示的内容都是一个空白的灰色框(由于引导类),没有标题或帖子内容,但正确数量的框显示了发布对象的数量。我错过了什么?
编辑:控制台显示没有错误,GET请求返回200 application/json。我有一个console.log($ scope.posts),它打印出[Resource, Resource, Resource, Resource, Resource, Resource, Resource, $promise: Object, $resolved: true]
。此外,
for (key in $scope.posts){
console.log(posts[key]);
}
打印出
Resource {url: "http://localhost:8000/api/posts/i-had-a-api-his-name-was-bingo/", id: 0, title: "I had a api... his name was Bingo", content: "E-I-E-I-O", owner: "Heather"…}
你在控制台中看到了什么?使用DevTools->网络 - >过滤XHR并查看返回的内容 – Angela 2014-09-04 06:24:33