1

我在使用 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'}"}]}

“样式”不适用于我的元素。有人可以帮我解决这个问题吗?

4

1 回答 1

0

基本上你已经对 JSON 进行了区分,在将它分配给之前先解析它ng-style

ng-style="doParseToJson(row.Style)"

代码

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

还要确保响应应该用"双引号而不是单引号括起来,'{"color":"#ffff00"}'以保持它在解析 JSON 时有效。

于 2016-03-23T11:04:41.647 回答