在 AngularJS 中,$filter
提供了一种格式化我们向用户显示的数据的方法。
然而,$interpolate
还允许我们实时更新字符串
的文本。
$interpolate
和 $filter
彼此相关吗?这两者在概念上有何不同?
最佳答案
您可以将 $interpolate() 视为专用过滤器。
var interpolationFunction = $interpolate('{{thing}} is {{color}}.');
var grass = {thing: 'Grass', color: 'green'};
console.log(interpolationFunction(grass));
// Or just.
console.log(interpolationFunction({thing: 'Milk', color: 'white'}));
console.log(interpolationFunction({thing: 'The sky', color: 'blue'}));
这将产生:
Grass is green.
Milk is white.
The sky is blue.
您可以将 $interpolate(STRING) 的返回值视为一个编译模板,稍后使用一组变量进行渲染。
相比之下,$filter(NAME) 返回一个先前注册为名称为 NAME 的过滤器的函数。例如,“大写”过滤器将其参数转换为大写,“数字”过滤器格式化数字,“日期”过滤器格式化日期对象,并且您可以定义自己的命名过滤器,以使用其参数执行任意操作。
关于javascript - AngularJS - $interpolate 与 $filter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39546316/