我有一个角度对象数组,我想要做的是迭代数组并添加一个我可以在ng-style中调用的新属性。我该如何添加这样的样式属性?
CommentService.GetComments(12535372).then(function () {
$scope.comments = CommentService.data();
angular.forEach($scope.comments, function (value, key) {
$scope.comments.push('displayPercent:' + 'margin-left:'+ value.percent + '%');
});
});
我的观点:
<div ng-repeat="comment in comments">
<div style="float: right;" ng-style="comment.displayPercent">
{{ comment.comment }}
</div>
</div>
答案 0 :(得分:1)
假设$ scope.comments是一个对象数组:
angular.forEach($scope.comments, function (value, key) {
value.displayPercent = {'margin-left': value.percent + '%'};
});