admin管理员组文章数量:1415476
Problem: I am having a hard time to replace"& ;" to &. Is their a way to use javascript to replace the ampersand to &.
<div class="col-md-4 no-margin padding" id="change">
<strong>Specialty: </strong>{{e.Specialty}}
</div>
I would like to replace the "& ;" when it grabs the data from the database to "&".
So currently, it will display "Nursing & ; Doctor" rather then "Nursing & Doctor"
How can I achieve this using replace. I have tried ng-show, Santize, and ng-bind-html but have not worked out for me.
Problem: I am having a hard time to replace"& ;" to &. Is their a way to use javascript to replace the ampersand to &.
<div class="col-md-4 no-margin padding" id="change">
<strong>Specialty: </strong>{{e.Specialty}}
</div>
I would like to replace the "& ;" when it grabs the data from the database to "&".
So currently, it will display "Nursing & ; Doctor" rather then "Nursing & Doctor"
How can I achieve this using replace. I have tried ng-show, Santize, and ng-bind-html but have not worked out for me.
Share Improve this question asked Sep 19, 2017 at 17:13 RobertRobert 1671 gold badge2 silver badges14 bronze badges4 Answers
Reset to default 2This is not necessarily the best way but for those of you that need to do this you can use this:
var parser = new DOMParser;
var dom = parser.parseFromString(YOURSTRING,'text/html');
var decodedString = dom.body.textContent;
You have to use $sce service and trustAsHtml so that Angular will not do the encoding and display the output without output encoding. Details are here https://docs.angularjs/api/ngSanitize
Steps are below-
- Include ngSanitize module - angular.module('app', ['ngSanitize']);
- Inject $sce service in your controller
- Truest output using - vm.Specialty= $sce.trustAsHtml(Specialty);
- Display to the user without output encoding - Specialty: bind-html-pile=e.Specialty
This can be done using
<span ng-bind-html="e.Specialty"></span>
and include ['ngSanitize']
in your module
plunkr example: https://plnkr.co/edit/d6Anjr7vL6un9d0yL5ZW?p=preview
I would use a filter...
JavaScript:
myApp.filter('andFilter', function() {
return function(item) {
return item.replace(/& ;/g, '&');
}
});
HTML:
<div class="col-md-4 no-margin padding" id="change">
<strong>Specialty: </strong>{{e.Specialty | andFilter}}
</div>
本文标签: javascriptHow to use replace ampamp with amp in angularjsStack Overflow
版权声明:本文标题:javascript - How to use replace &amp; with & in angularjs - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745158415a2645315.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论