0

在AngularJS中,$filter提供了一种格式化显示给用户的数据的方法。 但是,$interpolate也允许我们更新文字字符串 。

$interpolate$filter彼此相关?这两者在概念上有什么不同?

回答

1

您可以将$ 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. 

你能想到的$返回值的插值(STRING)作为模板编译您稍后有一组变量呈现。

相比之下,$ filter(NAME)返回一个以前注册为名称为NAME的过滤器的函数。例如,“大写”过滤器将其参数转换为大写形式,“数字”过滤器格式数字,“日期”过滤器格式日期对象,您可以定义自己的命名过滤器,使用其参数执行任意操作。