我有一个 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; 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/