我有一個簡單的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