当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


AngularJS ng-pluralize用法及代码示例


AngularJS中的ng-pluralize指令用于根据en-us本地化规则指定要显示的消息。 en-us本地化规则与AngularJS捆绑在一起。 AngularJS中的默认复数类别为“one”和“other”。

用法:

<ng-pluralize count="" when="string" [offset="number"]>
Contents...
</ng-pluralize>

参数值:


  • count:它指的是Angular表达式使用的count属性的值。
  • when:它用于指定实际字符串和复数类别之间的映射。该属性值必须为JSON对象样式。
  • offset:它指定要从总数中扣除的偏移量属性。

范例1:本示例使用ng-pluralize指令显示内容。

<!DOCTYPE html> 
<html> 
  
<head> 
    <title>ng-pluralize Directive</title> 
      
    <script src= 
"https://ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular.min.js"> 
    </script>  
</head> 
  
<body ng-app="app" style="padding:20px"> 
      
    <h1 style="color:green;">GeeksforGeeks</h1> 
    <h2 style="">ng-pluralize Directive</h2> 
      
    <div ng-controller="geek"> 
        <div ng-init="Hotel=[0, 1, 2, 3]"> 
            Choose from list:  
            <select ng-model="booking" ng-options= 
                "booking as booking for booking in Hotel"> 
            </select> 
              
            <br><br>  
              
            <ng-pluralize count="booking" when="{ 
                    '0':'No booking available', 
                    '1':'{{booking}} booking available', 
                    '2':'{{booking}} bookings available', 
                    '3':'{{booking}} bookings available', 
                    }"> 
            </ng-pluralize> 
        </div> 
    </div> 
      
    <script> 
        var app = angular.module("app", []); 
        app.controller('geek', ['$scope', function ($scope) { 
            $scope.booking = 0; 
        }]); 
    </script> 
</body> 
  
</html>                    

输出:
选择元素之前:
ngpluralize
选择元素后:
ngpluralize

范例2:本示例使用ng-pluralize指令显示输入文本内容。

<!DOCTYPE html> 
<html> 
  
<head> 
    <title>ng-pluralize Directive</title> 
      
    <script src= 
"https://ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular.min.js"> 
    </script>  
</head> 
  
<body ng-app="app" style="text-align:center"> 
      
    <h1 style="color:green;">GeeksforGeeks</h1> 
    <h2 style="">ng-pluralize Directive</h2> 
      
    <div ng-controller="geek"> 
        <p>Input Names separated by comma(, ):</p> 
        <textarea class="form-control" ng-model="people"
            ng-list=", "></textarea> 
          
        <br><br> 
          
        <ng-pluralize count="people.length" offset="2" when="{ 
                '0':'You got no viewers.', 
                '1':'{{people[0]}} is viewing.', 
                '2':'{{people[0]}} and {{people[1]}} are viewing.', 
                'one':'{{people[0]}}, {{people[1]}} and one other 
                        person is viewing.', 
                'other':'{{people[0]}}, {{people[1]}} and {} 
                        other people are viewing.'}"> 
            </ng-pluralize> 
    </div> 
      
    <script> 
        var app = angular.module("app", []); 
        app.controller('geek', ['$scope', function ($scope) { 
            $scope.people = []; 
        }]); 
    </script> 
</body> 
  
</html>

输出:
在输入文本之前:
ngpluralize
输入文字后:
ngpluralize



相关用法


注:本文由纯净天空筛选整理自Vishal Chaudhary 2大神的英文原创作品 AngularJS | ng-pluralize Directive。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。