【问题标题】:ng-bind with AngularJS translate interpolationng-bind 与 AngularJS 转换插值
【发布时间】:2018-05-21 22:07:52
【问题描述】:

我需要渲染诸如 之类的字符,它们可能包含在用户的名字或姓氏中。我正在使用角度翻译来呈现名称,将名字和姓氏作为值传递。但是,如果名字或姓氏包含 ng-bind="translation_key | translate | values" 之类的东西,但这是不正确的。如何将 ng-bind 与角度平移插值一起使用?这是呈现包含 等字符的翻译值的正确方法吗?谢谢。

*编辑

这几乎可以工作,但消毒剂会输出错误。

ng-bind="'locales.user_filter' | translate: { first: user.firstName, last: user.lastName }"

错误:[$sanitize:badparse] sanitizer 无法解析以下 html 块:

【问题讨论】:

    标签: angularjs angular-translate ng-bind


    【解决方案1】:

    在将值传递给翻译键之前,我使用了一个过滤器。

    .filter('htmlEntities', function () {
      return function (input) {
        return String(input).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
      };
    });
    
    
    var first = $filter('htmlEntities')(scope.user.firstName);
    var last = $filter('htmlEntities')(scope.user.lastName);
    scope.displayName = $translate.instant('locales.user_filter', { first: first, last: last });
    
    <td ng-bind-html="displayName"></td>
    

    【讨论】:

      猜你喜欢
      • 2013-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多