2016-03-23 29 views
1

我在使用ng-repeat的ng-style時遇到了問題。ng-repeat的AngularJS和ng-style

這是我的代碼:

<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(加載的服務器端從DB):

$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'}"}]} 

「樣式」 不被施加到我的元素。有人可以幫我解決這個問題嗎?

回答

0

基本上你已經strigified JSON,做分析之前將其分配到ng-style

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

代碼

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

還要確保反應應該與"雙qoutes,而不是單引號包裹例如'{"color":"#ffff00"}'以保持解析JSON的有效性。