i am having trouble with angular.js rendering json data with ng-repeat directives. I am currently working with Django REST Framework. Please see the source code below. Thanks.
REST API works very well. ( result via curl command )
user$ curl localhost:8000/posts/ -i
HTTP/1.0 200 OK
Date: Sat, 02 Aug 2014 15:37:27 GMT
Server: WSGIServer/0.1 Python/2.7.8
Vary: Accept, Cookie
X-Frame-Options: SAMEORIGIN
Content-Type: application/json
Allow: GET, POST, HEAD, OPTIONS
[{"id": 1, "title": "First Post", "content": "hello world, my first post"}, {"id": 2, "title": "this is my second post via POST request", "content": "should return 201 Created"}, {"id": 3, "title": "third post", "content": "why not working"}]%
app.js : very simple controller using $http service.
(function(){
var app = angular.module("PostApp", []);
app.controller("PostCtrl", ["$scope", "$http", function($scope, $http){
var store = this;
$http.get('/posts/')
.success(function(data){
$scope.datas = data;
console.log(data[0]);
});
}]);
})();
result of console.log(data[0]) is ... ( from Chrome Developer Tools > Console )
Object {id: 1, title: "First Post", content: "hello world, my first post"}
i am actually not sure about why this is an "Object".
view ( index.html )
<html ng-app="PostApp">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
{% load staticfiles %}
<title>Angular.js with Django</title>
</head>
<body ng-controller="PostCtrl as pc">
<h1>Welcome to Angular.JS</h1>
<div ng-repeat="data in datas">
<h3>{{ data.title }}</h3>
</div>
<script src="{% static 'posts/angular.js' %}"></script>
<script src="{% static 'posts/app.js' %}"></script>
</body>
</html>
Rendered HTML, which not i intended.
it did NOT rendered {{ data.title }}, ( h3 element is empty )
<html ng-app="PostApp" class="ng-scope"><head><style type="text/css">@charset "UTF-8";[ng\:cloak],[ng-cloak],[data-ng-cloak],[x-ng-cloak],.ng-cloak,.x-ng-cloak,.ng-hide{display:none !important;}ng\:form{display:block;}.ng-animate-block-transitions{transition:0s all!important;-webkit-transition:0s all!important;}.ng-hide-add-active,.ng-hide-remove{display:block!important;}</style>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Angular.js with Django</title>
</head>
<body ng-controller="PostCtrl as pc" class="ng-scope">
<h1>Welcome to Angular.JS</h1>
<!-- ngRepeat: data in datas --><div ng-repeat="data in datas" class="ng-scope">
<h3></h3>
</div><!-- end ngRepeat: data in datas --><div ng-repeat="data in datas" class="ng-scope">
<h3></h3>
</div><!-- end ngRepeat: data in datas --><div ng-repeat="data in datas" class="ng-scope">
<h3></h3>
</div><!-- end ngRepeat: data in datas -->
<script src="/static/posts/angular.js"></script>
<script src="/static/posts/app.js"></script>
</body></html>
what should i have to change to render appropriate json data in view. in this case {{ data.title }} ?