[英]angularjs - JS string containing interpolation
假设我的Web应用程序从后端收到json响应,如下所示:
[
{id:1, description: 'this is an example 1, **value is ###**'},
{id:2, description: 'this is an example 2, **value is ###**'},
{id:3, description: 'this is an example 3, **value is ###**'}
]
这里有一些格式语法。 带有**
文本表示粗体 。
这部分很容易。 我只需要搜索**
并将其替换为<b></b>
,然后在模板中使用ng-bind-html
。
现在有###
。 这意味着我必须用一些动态变化的变量来代替它。 如果整个字符串都是硬编码的,我们可以在模板中轻松地做到这一点:
<div>this is example 1, value is {{someVariable}}</div>
如何从Javascript构造这样的字符串?
可以使用自定义过滤器,例如:
angular.module('myApp')
.filter('htmlReplace', function() {
// `input` is the description string, otherVar is `someVariable` in view
return function(input, otherVar) {
let html = input.replace(...// do your replacing
return html;
}
})
视图
<div ng-bind-html="obj.description | htmlReplace: someVariable"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.