[英]How to interpolate string in Angular
我有一个angular Web应用程序,所以我想也将angular用于字符串插值。 例如,我想在myTemplate
进行一些替换:
var myTemplate = "Make something awesome with a = '{{scope.a}}' and b = '{{scope.b}}'.";
var scope = {
a: 1,
b: 2
};
我知道如何使用underscore.js:
_.template(myTemplate)(scope);
我想知道角度是否可行?
使用$ interpolate服务:
var myTemplate = "Make something awesome with a = '{{a}}' and b = '{{b}}'.";
var scope = {
a: 1,
b: 2
};
$interpolate(myTemplate)(scope); // "Make something awesome with a = '1' and b = '2'."
仅在具有上述scope
对象结构的情况下,才应使用{{a}}
而不是{{scope.a}}
。
是的,您可以使用angular的$interpolate
服务
var myTemplate = "Make something awesome with a = '{{a}}' and b = '{{b}}'.";
var fn = $interpolate(myTemplate);
var scope = {
a: 1,
b: 2
};
// Make something awesome with a = '1' and b = '2'.
fn(scope);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.