【问题标题】:Cannot display some special chars in ng-repeat无法在 ng-repeat 中显示一些特殊字符
【发布时间】:2017-04-25 09:37:39
【问题描述】:

我正在尝试在 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>

我遇到的这个问题是 $scope.test.value1 中包含的 \t 和 \ 没有被渲染。

我不想手动转义字符(使用 \\t 和 \\),因为我将从 REST 服务接收此数组。

我搜索了几个小时都没有成功(试过 $sce)。

这是我遇到的问题的一个小问题:https://plnkr.co/edit/AhJaNCOa0saGTSjh9u5H?p=preview

【问题讨论】:

    标签: angularjs json angularjs-ng-repeat backslash


    【解决方案1】:

    您面临转义序列问题,您需要使用额外的 \(反斜杠)转义每个特殊字符,这是您要在视图上打印的每个特殊字符的情况,试试这个(单击运行代码 sn-p 以查看输出):

    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>

    【讨论】:

    • 你总是需要转义特殊字符,你也可以从服务器发送转义值。
    • 谢谢。有没有办法(如函数)从我的对象数组中转义每个特殊字符而无需手动操作?
    • 你在服务器端使用什么?
    • 这是一个使用 Java 构建的 REST 服务
    猜你喜欢
    • 1970-01-01
    • 2016-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多