ng-pluralize with translations Error:“TypeError”

半世苍凉 提交于 2019-12-04 18:24:03

If you want async loading of translation string you need to recompile ng-pluralize.

Here is a demo: http://plnkr.co/edit/0pFdk4Ac1QC5SE7JSXeJ?p=preview

When the <ng-pluralize> element is first linked, translateStrings is unset. Try putting it into the $scope in a controller's constructor instead of on keypress.

Update:

Here's an example with a controller:

<script>
  // The controller will be injected with a new scope for your element.
  var TranslationController = function($scope) {
    // This is your code from your example. This assumes there's a global
    // 'userLocale' variable.
    if (userLocale == "en-us"){
      $scope.translateStrings = 
      {'0': '0 candy for sale','one': '1 candy for sale.','other': '{} candies for sale.'} ;
      debugger;
    }
    else if (userLocale == "de-de"){
      $scope.translateStrings = {'0': '0 Süßigkeiten ausgewählt',
      'one': '1 Süßigkeiten zum Verkauf',
      'other': '{} Süßigkeiten zum Verkauf.'
      };
      debugger;
    }
  };
</script>

And the HTML:

<div ng-controller="TranslationController">
  <input class="input-text" type="number" ng-model="candyCount" />
  <ng-pluralize count = "candyCount" when = "translateStrings" ></ng-pluralize>
</div>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!