?
? ????? PHP ??? ???? ??? ?? ??
ngPluralize
指令依照en-US本地化規(guī)則顯示信息。這些規(guī)則和angular.js捆綁在一起,但是可以被覆蓋(參見 Angular i18n 開發(fā)指南)。配置ngPluralize指令的方式是通過給出多元化條目和顯示字符串的映射實現(xiàn)的。
有兩個多元化條目在Angular的默認(rèn)en-US本地化中: "one"和"other"。
一個多元化條目可以匹配多個數(shù)字(例如,在en-US本地化中,"other"可以匹配除1以外的任何數(shù)字),一條顯式數(shù)字規(guī)則只能匹配一個數(shù)字,例如,顯式數(shù)字規(guī)則"3"只能匹配數(shù)字3。本文檔的后面有很多多元化條目和顯示數(shù)字規(guī)則的示例。
你可以使用2個屬性配置ngPluralize: count
和 when
。你還能使用可選的屬性offset
。
count
屬性的值可以是字符串或一個Angular表達(dá)式;它會在綁定值的當(dāng)前域上運算。
when
屬性指定多元化條目和實現(xiàn)顯示字符串的映射關(guān)系。屬性值可以是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."
是一條顯式數(shù)字規(guī)則。如果你未指定這條規(guī)則,0會被匹配到"other"條目,并且"0 people are viewing"會替代"Nobody is viewing"顯示出來。你可以為其它數(shù)字指定一條顯式數(shù)字規(guī)則,例如12,這樣可以替代"12 people are viewing"的顯示,你可以顯示成"a dozen people are viewing"。
你可以使用一對花括號({}
) 作為數(shù)字的占位符,將數(shù)字代入到多元化字符串中。在前一個例子中,Angular會替換{}
為 {{personCount}}
。成對花括號{}
作為{{numberExpression}}的點位符。
offset
屬性允許進(jìn)一步定制化多元化文本,可以產(chǎn)生一個更好的用戶體驗。例如,替代信息"4 people are viewing this document",你可以顯示為"John, Kate and 2 others are viewing this document"。offset屬性允許你使用任何所需的值來偏移一個數(shù)字。讓我們來看一個例子:
<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),但是我們增加了三條顯式數(shù)字規(guī)則 0, 1 和 2。當(dāng)一個人,也許是John,看了這個文檔,將會顯示"John is viewing"。當(dāng)三個人看了這個文檔,非顯式數(shù)字規(guī)則被找到,因為偏移2后為3,Angular使用1來檢索多元化條目。在這種情況下,多元化條目 'one'被匹配到,并且"John, Mary and one other person are viewing"被顯示。
注意當(dāng)你指定偏移時,你必須給從0到偏移值的數(shù)字提供顯式數(shù)字規(guī)則。例如,如果你使用偏移值3, 你必須提供顯式數(shù)字規(guī)則 0, 1, 2 和3。同時你也必須為多元化條目提供多元化字符串 "one" and "other"。
<ng-pluralize
count=""
when=""
[offset=""]>
...
</ng-pluralize>
<ANY
count=""
when=""
[offset=""]>
...
</ANY>
參數(shù) | 類型 | 詳述 |
---|---|---|
count | stringexpression | 要綁定的變量。 |
when | string | 多元化條目和對應(yīng)字符串的映射。 |
offset
(可選)
|
number | 從總數(shù)扣減的偏移值。 |
<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>
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.');});