【问题标题】:Angular.js How to render a HTML tag from json fileAngular.js 如何从 json 文件中呈现 HTML 标签
【发布时间】:2016-09-09 18:54:15
【问题描述】:

我正在尝试在我的角度页面中解析 html 值,但不确定我做错了什么,我在控制台中收到此错误:

app.js:13 Uncaught SyntaxError: Unexpected token )

app.js

var App = angular.module('App', []);

App.controller('ProjectCtrl', function($scope, $http) {
  $http.get('app/test.json')
       .then(function(res){
          $scope.projects = res.data;
        });

       App.filter('to_trusted', ['$sce', function($sce){
    return function(text) {
        return $sce.trustAsHtml(text);
    };
});

test.json

[
  { "icon": "<i class="fa fa-github fa-3x" aria-hidden="true"></i>", "name": "lovelycss" },
  { "icon": "<i class="fa fa-github fa-3x" aria-hidden="true"></i>", "name": "lovely-icons" }
]

html:

<div ng-controller="ProjectCtrl">
  <ul>
    <li ng-repeat="project in projects">
     <div ng-bind-html="'{{project.icon}}' | to_trusted"></div>- <em>{{project.name}}</em>
    </li>
  </ul>
</div>

我要归档的是:http://jsfiddle.net/JfGVE/1547/

我想要一个 json 加载图标和文本。

我希望现在这一切都清楚了。

【问题讨论】:

标签: angularjs json


【解决方案1】:

您的控制器声明中缺少},过滤器声明中缺少[

var App = angular.module('App', []);

App.controller('ProjectCtrl', function($scope, $http) {
    $http.get('app/test.json')
         .then(function(res){
            $scope.projects = res.data;
          });
}); // You are missing a '}' here

App.filter('to_trusted', ['$sce', function($sce){
    return function(text) {
        return $sce.trustAsHtml(text);
    };
}]); // You are missing a ']' here

您还必须编辑 JSON 以转义引号 "

[
    {
        "icon": "<i class=\"fa fa-github fa-3x\" aria-hidden=\"true\"></i>",
        "name": "lovelycss"
    }, {
        "icon": "<i class=\"fa fa-github fa-3x\" aria-hidden=\"true\"></i>",
        "name": "lovely-icons"
    }
]

您传递给ng-bind-html 的表达式也是错误的。由于单引号 ' 您将文字字符串 '{{project.icon}}' 传递给过滤器。您必须删除引号和花括号,因为ng-bind-html 指令需要一个表达式作为参数。

<div ng-controller="ProjectCtrl">
  <ul>
    <li ng-repeat="project in projects">
     <div ng-bind-html="project.icon | to_trusted"></div>- <em>{{project.name}}</em>
    </li>
  </ul>
</div>

【讨论】:

  • 谢谢,但我现在收到此错误:SyntaxError: Unexpected token f in JSON at position 25 at xc (angular.js:1326) at dc (angular.js:10515) at angular.js :10606 at r (angular.js:321) at hd (angular.js:10605) at c (angular.js:11403) at angular.js:16170 at m.$eval (angular.js:17444) at m. $digest (angular.js:17257) at m.scopePrototype.$digest (hint.js:1364)
  • @Raduken 那是因为你必须用这种方式用反斜杠来转义引号(“):\”我将编辑我的答案以添加它。
  • 谢谢,但在我的 html 中正在加载这个: {{project.icon}} ,公平地说,我想要那里呈现一个图标,明白吗?我不想显示 标签而是图标,明白吗?
  • @Raduken 我明白了。请编辑您的问题,提供所有信息以及您已完成的所有步骤。我将编辑我的答案以提供该问题的解决方案。
  • @Raduken 答案已编辑。我希望这是最终的解决方案:)
【解决方案2】:

该消息告诉您问题:您有语法错误。实际上是两个。

  1. 您的控制器的功能未关闭。
  2. 过滤器没有右括号。

app.js

var App = angular.module('App', []);

App.controller('ProjectCtrl', function($scope, $http) {
  $http.get('app/test.json')
       .then(function(res){
          $scope.projects = res.data;
        });
}); // 1

App.filter('to_trusted', ['$sce', function($sce){
    return function(text) {
        return $sce.trustAsHtml(text);
    };
}]); // 2

使用此方法修复 JSON 错误

[
  { "icon": "<i class='fa fa-github fa-3x' aria-hidden='true'></i>", "name": "lovelycss" },
  { "icon": "<i class='fa fa-github fa-3x' aria-hidden='true'></i>", "name": "lovely-icons" }
]

【讨论】:

  • 谢谢,但我现在收到此错误:SyntaxError: Unexpected token f in JSON at position 25 at xc (angular.js:1326) at dc (angular.js:10515) at angular.js: 10606 at r (angular.js:321) at hd (angular.js:10605) at c (angular.js:11403) at angular.js:16170 at m.$eval (angular.js:17444) at m.$摘要 (angular.js:17257) 在 m.scopePrototype.$digest (hint.js:1364)
  • 您在 json 的字符串内容中使用了双引号(这是不允许的,因为我们已经对字符串使用了双引号);查看编辑
  • 谢谢,但在我的 html 中正在加载这个: {{project.icon}} ,公平地说,我想要那里呈现一个图标,明白吗?我不想显示 标签而是图标,明白吗?
猜你喜欢
  • 2015-09-28
  • 1970-01-01
  • 2020-05-08
  • 2021-12-13
  • 2019-01-09
  • 2021-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多