【问题标题】:Angular sanitize / ng-bind-html not working?Angular sanitize / ng-bind-html 不起作用?
【发布时间】:2013-01-21 12:36:32
【问题描述】:

我已经设置了一个转发器,只要其中没有 html,就可以获取要显示的数据。

我已包含angular-sanitize.js 并尝试使用ng-bind-html

但在跨度内没有显示任何内容,仅在 ng-bind-html 属性内显示。所以看起来 sanitize 不起作用,

我读到这需要添加到应用程序依赖项中,但不确定在哪里添加。

我刚刚在 Angular 网站上完成了 tut,所以现在只设置了一个非常基本的控制器。

【问题讨论】:

  • 你能展示一下你在 jsfiddle 上的尝试吗
  • 看起来我使用的 angular-sanatize.js 文件不正确,我从 git repo 中获取了它,但是当我使用 js fiddle 上使用的那个时,它现在可以按预期工作了。

标签: angularjs ng-bind-html ngsanitize


【解决方案1】:

我的解决方案是从这里下载 js 文件

http://ajax.googleapis.com/ajax/libs/angularjs/1.0.3/angular-sanitize.js

我一直在使用我在 angular git repo 中找到的那个,

【讨论】:

    【解决方案2】:
    1. 您需要包含 angular-sanitize.js http://ajax.googleapis.com/ajax/libs/angularjs/1.0.3/angular-sanitize.js

    2. 将“ngSanitize”添加到您的模块依赖项中

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

    3. 不要在属性中使用{{}}

    4. 不要使用$sce.trustAsHtml()

    【讨论】:

    • 感谢您提供这些步骤。这样更容易理解和理解。
    • 对我来说也很完美。谢谢。
    • 谢谢。完美运行。
    • 这应该是公认的答案(有 100 多票,为什么不呢?!)
    • 4.确保你的同事没有偷偷溜进来item.title = $sce.trustAsHtml(item.title)
    【解决方案3】:

    我的解决方案与 Seglespaan 正好相反。是使用 Bower 版本的 Angular Sanitize。

    bower install angular-sanitize
    

    https://github.com/angular/bower-angular-sanitize

    【讨论】:

      【解决方案4】:

      我遇到了类似的问题,但我的有点奇怪。只有输入标签没有呈现,而其他所有内容包括

      <h3></h3>
      <em></em> 
      

      做到了。几个小时后,我意识到除了

      angular-sanitize.min.js 
      

      我需要添加

      textAngular-sanitize.min.js
      

      在输入标签起作用之前我的项目。这真的很令人沮丧,所以我希望这对处于类似情况的任何人都有帮助

      【讨论】:

        【解决方案5】:

        使用指令时遇到此问题,解决方案未在代码中使用“替换”。

        `ng-html-bind' 被用于 templateUrl 视图中的一个 div

        appDirectives.directive('helpText', [function () {
        return {
            restrict: 'E',
            //replace: true, // With this uncommented it does not work!
            scope: {
                displayText: '='
            },
            templateUrl: '/web/form/helptext',
            link: function (scope) {
        
            }
        
        };
        }]);
        

        【讨论】:

          猜你喜欢
          • 2017-04-03
          • 1970-01-01
          • 1970-01-01
          • 2015-12-22
          • 1970-01-01
          • 1970-01-01
          • 2014-02-17
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多