繁体   English   中英

如何在Angular中插值字符串

[英]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}}

演示: http//plnkr.co/edit/aoaeWekhkLP5k7NR2RWB?p = preview

是的,您可以使用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.

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