ng-pluralize“when”绑定到变量

时间:2014-03-11 23:46:00

标签: javascript angularjs

我见过的所有Angular示例都使用文字“when”参数演示ng-pluralize:

<ng-pluralize count="x" when="{'one': 'thing', 'other': 'things'}"></ng-pluralize>

是否可以使用范围变量作为when属性?例如:

<ng-pluralize count="x" when="things"></ng-pluralize>

其中$scope.things设置为{'one': 'thing', 'other': 'things'}

我已经尝试了它并且它不起作用,因为我在这里指定它,但我希望我只是遗漏了一些东西,这是可能的。

2 个答案:

答案 0 :(得分:3)

所以我发现,虽然您无法在when属性中有效地添加变量(您可以,但更新并未反映出来),您可以这样做:

<ng-pluralize count="modalUsers.length" 
  when="{ 'one': '{{ templateConfig.pluralizeConfig.one }}',
          'other': '{{ templateConfig.pluralizeConfig.other }}' }">
</ng-pluralize>

然后更新templateConfig对象。

答案 1 :(得分:2)

如果查看documentation for ng-pluralize,则表明“count”是字符串或表达式,但“when”只是一个字符串。我猜那时候这是不可能的。我会在一瞬间写一个plunker进行测试,但文档似乎很清楚。也许功能请求是有序的? :)

Plunker - 正如所承诺的那样,您可以use this plunker查看字符串版本是否有效,但无论您如何更改它,使用变量的等效版本都无效!