使用ForEach on Object添加ng-style

时间:2014-03-22 01:25:48

标签: javascript angularjs angularjs-ng-repeat

我有一个角度对象数组,我想要做的是迭代数组并添加一个我可以在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>

1 个答案:

答案 0 :(得分:1)

假设$ scope.comments是一个对象数组:

angular.forEach($scope.comments, function (value, key) {
  value.displayPercent = {'margin-left': value.percent + '%'};
});