Javascript AngularJS 1.0和1.2之间的$interpolate差异

Javascript AngularJS 1.0和1.2之间的$interpolate差异,javascript,angularjs,interpolation,angular-filters,Javascript,Angularjs,Interpolation,Angular Filters,我正在编写一个过滤器,将地址格式化为一行。将传递到筛选器的对象的格式为: { Line1: "123 Main St.", Line2: "Apartment 2", // Optional City: "Chicago", State: "IL", Zip: "60623" } 到目前为止,我有以下几点: angular.module('myApp') .filter('address', function ($interpolate) { return fu

我正在编写一个过滤器,将地址格式化为一行。将传递到筛选器的对象的格式为:

{
  Line1: "123 Main St.",
  Line2: "Apartment 2", // Optional
  City: "Chicago",
  State: "IL",
  Zip: "60623"
}
到目前为止,我有以下几点:

angular.module('myApp')
  .filter('address', function ($interpolate) {
    return function (input, template) {

      if (input === null || !angular.isDefined(input)) {
        return input;
      }

      // template is optional. If not provided, use the following    
      if(!template) {
        template = '{{Line1}}, {{Line2 ? Line2 + \', \' : \'\'}}{{City}} {{State}} {{Zip}}';
      }

      try {
        var parsedTemplate = $interpolate(template);
      } catch (e) {
        console.log(parsedTemplate, template, input, e)
        return input;
      }

      // Compile the template in the context of the input object
      return parsedTemplate(input);
    };
  });
在Angular 1.2中,这可以正常工作。然而,在Angular 1.0中,它失败了,错误为:Lexer错误:表达式[Line2?Line2+,':''中第6-6列[?]处出现意外的下一个字符。我认为Angular 1.0不支持三元运算符
$interpolate
d表达式,但我找不到任何文档表明Angular 1.2中添加了支持

有没有办法在Angular 1.0中使用三元运算符,如果没有,我如何绕过这个限制


(优点-文档中哪里提到了这一更改,或者Angular git repo中的哪个commit进行了更改?

我意识到在升级到1.1.5之前,我在插值表达式中使用三元运算符的解决方法是使用
&
|
(如
someCondition&&TruthyResult | | FalseyResult
)以有效地获得相同的结果。以下是如何将其应用于代码:

template = '{{Line1}}, {{Line2 && (Line2 + \', \') || \'\'}}{{City}} {{State}} {{Zip}}';
演示:


此设置的唯一问题是,如果
TruthyResult
实际上没有返回真实的内容,那么将返回
FalseyResult
(与三元运算符相比,这只是像这样使用
&&
|
的性质)永远不会是错误的,因为
\',\'
,所以这里不会有问题。但在更一般的情况下,它可能是错误的。

我想我在另一个StackOverflow帖子上找到了提交,该帖子在Angular 1.1.5(提交)中添加了三元运算符使用三元运算符的另一种替代方法是:
someCondition&&DoTruthy | | DoFalsey
@Ian我相信这就是问题的答案;也许你应该把它作为一个答案而不是一个注释来写?根据,对三元运算符的支持确实是在“$parse:add support for ternal operators to parser()”中添加的所以,这是一个提交:。