AngularJS 和 ng-style with ng-repeat

AngularJS and ng-style with ng-repeat

我在将 ng-style 与 ng-repeat 结合使用时遇到问题。

这是我的代码:

<div style="display:table-row" ng-repeat="row in Data.Communications" id="{{row.Guid}}">
    <div style="display:table-cell;">
      <img ng-src="{{row.Path}}" ng-show="row.Path" ng-style="row.Style" />
      <i class="{{row.Icon}}" ng-show="row.Icon" ng-style="row.Style"></i>
    </div>
</div>

这是 JSON(从数据库加载服务器端):

$scope.Data: {"LastUpdate":"23/03/2016 11:45","Communications":[{"Guid":"2","Path":null,"Icon":"fa fa-warning","Style":"{'color':'#ffff00'}"},{"Guid":"1","Path":null,"Icon":"fa fa-warning","Style":"{'color':'#ffff00'}"},{"Guid":"3","Path":"/images/blink_yellow.gif","Icon":null,"Style":"{'width':'15px', 'height':'15px'}"}]}

"Style" 未应用于我的元素。有人可以帮我解决这个问题吗?

基本上你已经 strigfied JSON,在将它分配给 ng-style

之前先解析它
ng-style="doParseToJson(row.Style)"

代码

$scope.doParseToJson = function(style){
   return JSON.parse(style);
}

还要确保响应应该用 " 双引号而不是像 '{"color":"#ffff00"}' 这样的单引号括起来,以保持它对解析 JSON.

有效