简体   繁体   English

将字符串中的特定单词更改为粗体

[英]Change specific words of string to bold

I have aa single span element in my page. 我的页面中只有一个span元素。 I am concatenating words to a single variable in AngularJS and then referencing the variable to the span element in the page. 我将单词连接到AngularJS中的单个变量,然后将该变量引用到页面中的span元素。

$scope.abc = "Downstream";
$scope.abc.concat('<b>','Upstream',</b>);

When the page is viewed, it displays Downstream<b>Upstream</b> 查看页面时,它显示“ Downstream<b>Upstream</b>

How to display the word Upstream alone in Bold? 如何仅以粗体显示单词Upstream?

Seems a duplicate to angular variable generating html . 似乎重复到生成html的角度变量

I don't know angular, but reading that post it's easy. 我不了解角度,但是阅读该帖子很容易。 Just do: 做就是了:

 $scope.abc = $scope.trustAsHtml($scope.abc);

HTML 的HTML

<div data-ng-bind-html="abc "></div>     

Also check your Angular version, for version 1.2 or lower you could use the ng-bind-html-unsafe binding. 另外,请检查您的Angular版本,对于1.2或更低版本,您可以使用ng-bind-html-unsafe绑定。

<div class="post-content" ng-bind-html-unsafe="abc"></div>

This does not work anymore at Angular 1.2+ According to comment of TheSharpieOne : 根据TheSharpieOne评论,这在Angular 1.2+上不再TheSharpieOne

"It has been replaced with Strict Contextual Escaping. See docs.angularjs.org/api/ng.$sce " “已将其替换为严格的上下文转义。请参阅docs.angularjs.org/api/ng.$sce

according to this https://docs.angularjs.org/api/ng/directive/ngBindHtml 根据这个https://docs.angularjs.org/api/ng/directive/ngBindHtml

You need to include the ngBindHtml and $sanitize service 您需要包括ngBindHtml和$ sanitize服务

In your js file, it should be 在您的js文件中,应该是

angular.module('App', ['ngSanitize'])
  .controller('Ctr', ['$scope', function($scope) {
     $scope.abc = "Downstream";
     $scope.abc2 = $scope.abc.concat('<b>','Upstream','</b>');
}]);

In your html file, it should be 在您的html文件中,应该是

<div ng-controller="Ctr">
    <p ng-bind-html="abc2"></p>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM