2017-04-25 37 views
2

我試圖在ng-repeat內顯示一個對象數組。無法在ng-repeat中顯示一些特殊字符

這裏是對象

$scope.test = [ 
    { 
     value1: "app\test\maintenance1", 
     value2: "other value1" 
    }, 
    { 
     value1: "app\test\maintenance2", 
     value2: "other value2" 
    } 
] 

這裏的數組是HTML:

<table> 
    <tbody> 
     <tr ng-repeat="item in test"> 
      <td>{{item.value1}}</td> 
      <td>{{item.value2}}</td> 
     </tr> 
    </tbody> 
<table> 

這個問題我有是,\ t與\包含在$ scope.test.value1不渲染。

我不想手動轉義字符(使用\\ t和\\),因爲我將從REST服務接收此數組。

我搜索了幾個小時沒有成功(試過$ sce)。

這是問題的一個plunker我:https://plnkr.co/edit/AhJaNCOa0saGTSjh9u5H?p=preview

回答

1

您所面臨的轉義序列問題,你需要逃避與附加\(反斜線)每一個特殊字符,這是你想要的每一個特殊字符的情況下上打印圖,試試這個(點擊運行代碼片段,看看輸出):

angular.module('mainMod', []).controller('mainController', function($scope){ 
 

 
$scope.test = [ 
 
    { 
 
     value1: "app\\test\\maintenance1", 
 
     value2: "other value1" 
 
    }, 
 
    { 
 
     value1: "app\\test\\maintenance2", 
 
     value2: "other value2" 
 
    } 
 
] 
 

 
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.0/angular.min.js"></script> 
 
<html ng-app="mainMod"> 
 

 
<head> </head> 
 
<body ng-controller="mainController"> 
 

 
<ul> 
 
    <li ng-repeat="item in test"> 
 
     {{item.value1}} {{item.value2}} 
 
    </li> 
 

 
</ul> 
 

 
</body> 
 
</html>

+0

你總是需要轉義特殊字符,你可以從服務器發送逃脫值好。 – user3597009

+0

謝謝。有沒有一種方法(如函數)從我的對象數組中轉義每個特殊字符而不需要手動執行? – Jerebenz

+0

你在服務器端使用什麼? – user3597009