javascript - 我的 JSON 中的 & 在 Angular View 中呈现为 & ?

标签 javascript angularjs json special-characters angular-translate

我有一个 json 文件,用于存储我的 key /字符串,以便使用 Angular 翻译进行本地化。我在 View 中使用了一个字符串“配置文件和首选项”。当我使用 ng-bind-html 添加该字符串时,该字符串显示为:

个人资料和偏好

我认为 ng-bind-html 应该将其转换为 &。如何让它显示 & 而不是 &?我尝试将 json 中的字符串值更改为 & 但随后它仅显示为:

个人资料&首选项

这是我正在使用的标签:

<h3 ng-bind="'REGISTRATION_3.profile-preferences' | translate"></h3>

我也尝试过 With ng-bind-html-unsafe removed, how do I inject HTML? 中的建议.

当我使用上述链接问题中建议的过滤器时:

  app.filter('html', ['$sce', function ($sce) { 
    return function (text) {
        return $sce.trustAsHtml(text);
    };    
}]);

我得到这个结果:

Profile &amp;amp; Preferences

我还验证了我正在我的应用程序中注入(inject)“ngSanitize”。

最佳答案

我发现了这一点,当我选择“ng-bind”而不是“ng-bind-html”时,我输入得太快并且被切换。我的错误。

有效的方法是在 app.js 中使用此过滤器:

  app.filter('html', ['$sce', function ($sce) { 
    return function (text) {
        console.log('text is ');
        console.log(text);
        return $sce.trustAsHtml(text);
    };    
}]);

并在我的标签中使用它,如下所示:

<h3 ng-bind-html="'REGISTRATION_3.profile-preferences' | translate | html"></h3>

关于javascript - 我的 JSON 中的 & 在 Angular View 中呈现为 & ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41401150/

相关文章:

javascript - 如何从构建中排除 .js.map?

javascript - AngularJs ng-repeat 无法正常工作

javascript - 如何在 Angular JS 中根据条件更改图像?

node.js - 无法使用 Express + jade + Angular 查找 View

python JSON 到 dict 检查是否是单个或多个 child

c# - 从 json 文件制作动态字典

javascript - 更改 jcrop 选择的大小

javascript - 在文本区域元素上使用 onload

javascript - 与数组和字符串的instanceof混淆

java - JSON 解析错误 - 发布 json 时数据类型错误