[英]AngularJS ng-repeat is not looping through array
I am trying to build a loop with ng-repeat but can't iterate over an array of objects. 我正在尝试使用ng-repeat构建循环,但无法遍历对象数组。 It seems like the loop is not running at all.
似乎循环根本没有运行。
I started a jsfiddle - http://jsfiddle.net/a2xhzbfm/1/ 我开始了jsfiddle- http://jsfiddle.net/a2xhzbfm/1/
Below is an example of data ( $scope.postData
) that I'm working with: 以下是我正在使用的数据示例(
$scope.postData
):
[
{
"Title" : "Test1",
"Content" : "<div class=\"ExternalClass53DA82296DEE49898B93B8E59F9074BD\"><p>This is a test post.</p></div>",
"ImagePath" : "url",
"Text" : "Apple",
"Link" : "http://www.apple.com/",
"Created" : "/Date(1436889599000)/"
}, {
"Title" : "Test2",
"Content" : "<div class=\"ExternalClass53DA82296DEE49898B93B8E59F9074BD\"><p>This is a test post.</p></div>",
"ImagePath" : "url2",
"Text" : "Apple2",
"Link" : "http://www.apple.com/",
"Created" : "/Date(1436889599000)/"
}
]
In the JS I'm calling an api that returns a JSON and then storing that in $scope.postData
variable. 在JS中,我正在调用一个返回JSON的api,然后将其存储在
$scope.postData
变量中。
var app = angular.module('blogApp',[]);
app.controller('blogController', function($scope){
console.log('Angular is go!');
$scope.postData;
$.ajax({
url: 'someURL',
headers: { "Accept": "application/json; odata=verbose" },
success: function (data) {
$scope.postData = data.d.results;
console.log($scope.postData);
},
error: function (data) {
console.log(data.responseJSON.error);
}
});
});
Here is what I have for HTML so far. 到目前为止,这是我对HTML的了解。 I'll add more divs once I can get the data to print.
一旦可以打印数据,我将添加更多的div。
<body ng-app="blogApp">
<div ng-controller="blogController" class="container">
<div class="row">
<div ng-repeat="post in postData">
<div ng-repeat="(key, val) in post">
{{key}}:{{val}}There has to be something else!!!
</div>
</div>
</div>
</div>
</body>
Thanks in advance! 提前致谢!
It's because you're using jQuery for the Ajax. 这是因为您将jQuery用于Ajax。 Angular doesn't know that you changed
$scope.postData
. Angular不知道您更改了
$scope.postData
。 If you use Angular's own Ajax service, $http
, it will automatically handle updating the view after you make changes to $scope
. 如果您使用Angular自己的Ajax服务
$http
,则在对$scope
进行更改后,它将自动处理视图的更新。 But since Angular doesn't have any control over jQuery, there's no way for it to detect the update. 但是由于Angular无法控制jQuery,因此无法检测到更新。
The right way to fix this is to use $http
, but a wrong way that should work is to call $scope.$apply()
in your success callback right after you modify $scope.postData
. 解决此问题的正确方法是使用
$http
,但错误的工作方法是在修改$scope.postData
之后立即在成功回调中调用$scope.$apply()
。
Try switching your Ajax call to Angular's own $http function like so: 尝试将您的Ajax调用切换到Angular自己的$ http函数,如下所示:
$http.get('someURL', {
headers: {
"Accept": "application/json; odata=verbose"
}
}).success(function(data) {
$scope.postData = data.d.results;
console.log($scope.postData);
});
It's always recommended to use Angular's built in functions unless you need to use external libraries for cases where these functions aren't enough. 始终建议使用Angular的内置函数,除非在这些函数不够用的情况下需要使用外部库。
1 ) First you need to update version of AngularJS 1)首先,您需要更新AngularJS的版本
2 ) You have a misprint not in post but postData 2)您的打印错误不是在post而是在postData
check it: 核实:
<div ng-repeat="(key, val) in postData">
{{key}}:{{val}}There has to be something else!!!
</div>
http://jsfiddle.net/STEVER/a2xhzbfm/3/ http://jsfiddle.net/STEVER/a2xhzbfm/3/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.