Javascript angularjs-包含插值的JS字符串
假设我的web应用程序从后端获得json响应,如下所示:Javascript angularjs-包含插值的JS字符串,javascript,angularjs,string-interpolation,Javascript,Angularjs,String 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 ###**'} ] 这里有一些格式化语法。被**包围
[
{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 ###**'}
]
这里有一些格式化语法。被**
包围的文本表示粗体
这部分很简单。我只需要搜索**
并将其替换为
,然后在模板中使用ng bind html
现在有了###
。这意味着我必须用一些动态变化的变量来替换它。如果整个字符串都是硬编码的,那么我们可以在模板中轻松地执行此操作:
这是示例1,值为{{someVariable}}
如何从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;
}
})
看法
请根据您在问题中共享的数组共享输出。另外,这个逻辑还应该包含哪些其他输入?实际上,替换
###
比替换{{someVariable}
简单得多
<div ng-bind-html="obj.description | htmlReplace: someVariable"></div>