我正在尝试检索服务器发送的数据(JSON文件)。但是为什么它没有在我的浏览器中打印?
这是代码。
newsfeed.js
var newsfeed = angular.module('newsfeed',[]);
newsfeed.controller('newsfeedController',function($scope,$http){
$scope.posts = function(){
$http.get('http://localhost/must_sns/main/all_status').success(function(data){
$scope.posts = data;
});
}
});
html
<div ng-app="newsfeed" ng-controller="newsfeedController">
<ul>
<li ng-repeat="post in posts">
<p>{{post.body}}</p>
</li>
</ul>
</div>
抱歉,我对angular不太熟悉。
您首先将$ scope.posts设置为一个函数,但没有调用该函数。这意味着您的$ http请求永远不会被调用。尝试:
newsfeed.controller('newsfeedController',function($scope,$http){
var getPosts = function(){
$http.get('http://localhost/must_sns/main/all_status').success(function(data){
$scope.posts = data;
});
}
getPosts(); // need to call the function
});
(或者,您可以放弃getPosts函数,而直接调用$ http.get,但是最好将其保留在函数中,以防您以后需要再次调用它。)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句