annuaire recherche
AngularJS API Reference auto auto/service auto/service/$injector auto/service/$provide ng ng/directive ng/directive/a ng/directive/form ng/directive/input ng/directive/input[checkbox] ng/directive/input[date] ng/directive/input[dateTimeLocal] ng/directive/input[email] ng/directive/input[month] ng/directive/input[number] ng/directive/input[radio] ng/directive/input[text] ng/directive/input[time] ng/directive/input[url] ng/directive/input[week] ng/directive/ngApp ng/directive/ngBind ng/directive/ngBindHtml ng/directive/ngBindTemplate ng/directive/ngBlur ng/directive/ngChange ng/directive/ngChecked ng/directive/ngClass ng/directive/ngClassEven ng/directive/ngClassOdd ng/directive/ngClick ng/directive/ngCloak ng/directive/ngController ng/directive/ngCopy ng/directive/ngCsp ng/directive/ngCut ng/directive/ngDblclick ng/directive/ngDisabled ng/directive/ngFocus ng/directive/ngForm ng/directive/ngHide ng/directive/ngHref ng/directive/ngIf ng/directive/ngInclude ng/directive/ngInit ng/directive/ngKeydown ng/directive/ngKeypress ng/directive/ngKeyup ng/directive/ngList ng/directive/ngModel ng/directive/ngModelOptions ng/directive/ngMousedown ng/directive/ngMouseenter ng/directive/ngMouseleave ng/directive/ngMousemove ng/directive/ngMouseover ng/directive/ngMouseup ng/directive/ngNonBindable ng/directive/ngOpen ng/directive/ngPaste ng/directive/ngPluralize ng/directive/ngReadonly ng/directive/ngRepeat ng/directive/ngSelected ng/directive/ngShow ng/directive/ngSrc ng/directive/ngSrcset ng/directive/ngStyle ng/directive/ngSubmit ng/directive/ngSwitch ng/directive/ngTransclude ng/directive/ngValue ng/directive/script ng/directive/select ng/directive/textarea ng/filter ng/filter/currency ng/filter/date ng/filter/filter ng/filter/json ng/filter/limitTo ng/filter/lowercase ng/filter/number ng/filter/orderBy ng/filter/uppercase ng/function ng/function/angular.bind ng/function/angular.bootstrap ng/function/angular.copy ng/function/angular.element ng/function/angular.equals ng/function/angular.extend ng/function/angular.forEach ng/function/angular.fromJson ng/function/angular.identity ng/function/angular.injector ng/function/angular.isArray ng/function/angular.isDate ng/function/angular.isDefined ng/function/angular.isElement ng/function/angular.isFunction ng/function/angular.isNumber ng/function/angular.isObject ng/function/angular.isString ng/function/angular.isUndefined ng/function/angular.lowercase ng/function/angular.module ng/function/angular.noop ng/function/angular.toJson ng/function/angular.uppercase ng/object ng/object/angular.version ng/provider ng/provider/$animateProvider ng/provider/$compileProvider ng/provider/$controllerProvider ng/provider/$filterProvider ng/provider/$httpProvider ng/provider/$interpolateProvider ng/provider/$locationProvider ng/provider/$logProvider ng/provider/$parseProvider ng/provider/$rootScopeProvider ng/provider/$sceDelegateProvider ng/provider/$sceProvider ng/service ng/service/$anchorScroll ng/service/$animate ng/service/$cacheFactory ng/service/$compile ng/service/$controller ng/service/$document ng/service/$exceptionHandler ng/service/$filter ng/service/$http ng/service/$httpBackend ng/service/$interpolate ng/service/$interval ng/service/$locale ng/service/$location ng/service/$log ng/service/$parse ng/service/$q ng/service/$rootElement ng/service/$rootScope ng/service/$sce ng/service/$sceDelegate ng/service/$templateCache ng/service/$timeout ng/service/$window ng/type ng/type/$cacheFactory.Cache ng/type/$compile.directive.Attributes ng/type/$rootScope.Scope ng/type/angular.Module ng/type/form.FormController ng/type/ngModel.NgModelController ngAnimate ngAnimate/provider ngAnimate/provider/$animateProvider ngAnimate/service ngAnimate/service/$animate ngCookies ngCookies/service ngCookies/service/$cookies ngCookies/service/$cookieStore ngMessages ngMessages/directive ngMessages/directive/ngMessage ngMessages/directive/ngMessages ngMock ngMock/function ngMock/function/angular.mock.dump ngMock/function/angular.mock.inject ngMock/function/angular.mock.module ngMock/object ngMock/object/angular.mock ngMock/provider ngMock/provider/$exceptionHandlerProvider ngMock/service ngMock/service/$exceptionHandler ngMock/service/$httpBackend ngMock/service/$interval ngMock/service/$log ngMock/service/$timeout ngMock/type ngMock/type/angular.mock.TzDate ngMockE2E ngMockE2E/service ngMockE2E/service/$httpBackend ngResource ngResource/service ngResource/service/$resource ngRoute ngRoute/directive ngRoute/directive/ngView ngRoute/provider ngRoute/provider/$routeProvider ngRoute/service ngRoute/service/$route ngRoute/service/$routeParams ngSanitize ngSanitize/filter ngSanitize/filter/linky ngSanitize/service ngSanitize/service/$sanitize ngTouch ngTouch/directive ngTouch/directive/ngClick ngTouch/directive/ngSwipeLeft ngTouch/directive/ngSwipeRight ngTouch/service ngTouch/service/$swipe
personnages

AngularJS: API: ng/directive/ngPluralize

ngPluralize

  1. - directive in module ng

ngPluralize指令依照en-US本地化规则显示信息。这些规则和angular.js捆绑在一起,但是可以被覆盖(参见 Angular i18n 开发指南)。配置ngPluralize指令的方式是通过给出多元化条目和显示字符串的映射实现的。

多元化条目和显式数字规则

有两个多元化条目在Angular的默认en-US本地化中: "one"和"other"。

一个多元化条目可以匹配多个数字(例如,在en-US本地化中,"other"可以匹配除1以外的任何数字),一条显式数字规则只能匹配一个数字,例如,显式数字规则"3"只能匹配数字3。本文档的后面有很多多元化条目和显示数字规则的示例。

配置ngPluralize

你可以使用2个属性配置ngPluralize: countwhen。你还能使用可选的属性offset

count属性的值可以是字符串或一个Angular表达式;它会在绑定值的当前域上运算。

when属性指定多元化条目和实现显示字符串的映射关系。属性值可以是JSON对象。

下面的例子演示如何配置ngPluralize:

<ng-pluralize count="personCount"
                 when="{'0': 'Nobody is viewing.',
                     'one': '1 person is viewing.',
                     'other': '{} people are viewing.'}">
</ng-pluralize>

在例子中, "0: Nobody is viewing." 是一条显式数字规则。如果你未指定这条规则,0会被匹配到"other"条目,并且"0 people are viewing"会替代"Nobody is viewing"显示出来。你可以为其它数字指定一条显式数字规则,例如12,这样可以替代"12 people are viewing"的显示,你可以显示成"a dozen people are viewing"。

你可以使用一对花括号({}) 作为数字的占位符,将数字代入到多元化字符串中。在前一个例子中,Angular会替换{}{{personCount}}。成对花括号{}作为{{numberExpression}}的点位符。

使用offset配置ngPluralize

offset 属性允许进一步定制化多元化文本,可以产生一个更好的用户体验。例如,替代信息"4 people are viewing this document",你可以显示为"John, Kate and 2 others are viewing this document"。offset属性允许你使用任何所需的值来偏移一个数字。让我们来看一个例子:

<ng-pluralize count="personCount" offset=2
              when="{'0': 'Nobody is viewing.',
                     '1': '{{person1}} is viewing.',
                     '2': '{{person1}} and {{person2}} are viewing.',
                     'one': '{{person1}}, {{person2}} and one other person are viewing.',
                     'other': '{{person1}}, {{person2}} and {} other people are viewing.'}">
</ng-pluralize>

注意这里仍然使用了两条多元化条目(one, other),但是我们增加了三条显式数字规则 0, 1 和 2。当一个人,也许是John,看了这个文档,将会显示"John is viewing"。当三个人看了这个文档,非显式数字规则被找到,因为偏移2后为3,Angular使用1来检索多元化条目。在这种情况下,多元化条目 'one'被匹配到,并且"John, Mary and one other person are viewing"被显示。

注意当你指定偏移时,你必须给从0到偏移值的数字提供显式数字规则。例如,如果你使用偏移值3, 你必须提供显式数字规则 0, 1, 2 和3。同时你也必须为多元化条目提供多元化字符串 "one" and "other"。

指令信息

  • 这个指令执行优先级为0.

用法

  • 作为元素使用: (这个指令能用于自定义元素,但要注意IE限制)。
    <ng-pluralize
      count=""
      when=""
      [offset=""]>
    ...
    </ng-pluralize>
  • 作为属性使用:
    <ANY
      count=""
      when=""
      [offset=""]>
    ...
    </ANY>

参数

参数 类型 详述
count stringexpression

要绑定的变量。

when string

多元化条目和对应字符串的映射。

offset
(可选)
number

从总数扣减的偏移值。

示例

index.html
<script>
  angular.module('pluralizeExample', [])
    .controller('ExampleController', ['$scope', Function($scope) {
      $scope.person1 = 'Igor';
      $scope.person2 = 'Misko';
      $scope.personCount = 1;
    }]);
</script>
<div ng-controller="ExampleController">
  Person 1:<input Type="text" ng-model="person1" value="Igor" /><br/>
  Person 2:<input Type="text" ng-model="person2" value="Misko" /><br/>
  Number of People:<input Type="text" ng-model="personCount" value="1" /><br/>

  <!--- Example with simple pluralization rules for en locale --->
  Without Offset:
  <ng-pluralize count="personCount"
                when="{'0': 'Nobody is viewing.',
                       'one': '1 person is viewing.',
                       'other': '{} people are viewing.'}">
  </ng-pluralize><br>

  <!--- Example with offset --->
  With Offset(2):
  <ng-pluralize count="personCount" offset=2
                when="{'0': 'Nobody is viewing.',
                       '1': '{{person1}} is viewing.',
                       '2': '{{person1}} and {{person2}} are viewing.',
                       'one': '{{person1}}, {{person2}} and one other person are viewing.',
                       'other': '{{person1}}, {{person2}} and {} other people are viewing.'}">
  </ng-pluralize>
</div>
protractor.js
it('should show correct pluralized string', Function() {
  var withoutOffset = element.all(by.css('ng-pluralize')).get(0);
  var withOffset = element.all(by.css('ng-pluralize')).get(1);
  var countInput = element(by.model('personCount'));

  expect(withoutOffset.getText()).toEqual('1 person is viewing.');
  expect(withOffset.getText()).toEqual('Igor is viewing.');

  countInput.clear();
  countInput.sendKeys('0');

  expect(withoutOffset.getText()).toEqual('Nobody is viewing.');
  expect(withOffset.getText()).toEqual('Nobody is viewing.');

  countInput.clear();
  countInput.sendKeys('2');

  expect(withoutOffset.getText()).toEqual('2 people are viewing.');
  expect(withOffset.getText()).toEqual('Igor and Misko are viewing.');

  countInput.clear();
  countInput.sendKeys('3');

  expect(withoutOffset.getText()).toEqual('3 people are viewing.');
  expect(withOffset.getText()).toEqual('Igor, Misko and one other person are viewing.');

  countInput.clear();
  countInput.sendKeys('4');

  expect(withoutOffset.getText()).toEqual('4 people are viewing.');
  expect(withOffset.getText()).toEqual('Igor, Misko and 2 other people are viewing.');});
it('should show data-bound names', Function() {
  var withOffset = element.all(by.css('ng-pluralize')).get(1);
  var personCount = element(by.model('personCount'));
  var person1 = element(by.model('person1'));
  var person2 = element(by.model('person2'));
  personCount.clear();
  personCount.sendKeys('4');
  person1.clear();
  person1.sendKeys('Di');
  person2.clear();
  person2.sendKeys('Vojta');
  expect(withOffset.getText()).toEqual('Di, Vojta and 2 other people are viewing.');});
Article précédent: Article suivant: