我的目标是使 Angular 表达式的一部分变为粗体。
所以基本上我正在获取一个对象 obj
,并将其转换为字符串 str
。
obj = $scope.gridOptions1.api.getFilterModel();
for (var propt in obj) {
str += (" " + propt + ': ' + obj[propt]);
}
然后我需要在该对象中找到一个特定的词,例如“Depot”。我正在使用 str.indexOf("Depot")
执行此操作。我将其设置为变量 d
:
for (i = 0; i < str.length; i++) {
var d = str.indexOf("Depot");
}
然后我用粗体“Depot”替换原来的“Depot”:
if (d !== -1 ) {
var depot = str.substring(d, d + 5);
finalString = str.replace(depot, depot.bold());
}
然后我将我的字符串发射到示波器上:
$scope.getFiltered = finalString;
.bold() 方法只是在我的 UI 中返回带有“Depot”周围粗体标记的字符串。
所以我认为该字符串不被视为 HTML, 还有其他方法吗?
最佳答案
you will need to tell angular to treat the result as html. In order to do this, you need to include ngSanitize as a module dependency and insert the result with ng-bind-html.
所以html看起来像
<p ng-bind-html="htmlString"></p>
和 Angular 部分
angular.module('app', ['ngSanitize'])
关于javascript - AngularJS:以粗体显示表达式的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32885480/