【问题标题】:ng-pluralize hide element when value is 0 (zero)当值为 0(零)时,ng-pluralize 隐藏元素
【发布时间】:2026-02-12 05:00:01
【问题描述】:

我想使用 Angulars ng-pluralize 来隐藏/显示/更改元素,使其值为 0,该元素应该被隐藏(如 ng-hide="value == 0"ng-if="value != 0")。

当值为 1 时,元素应该有一个文本,如果它大于 1,它应该有一个文本。该值永远不能为负。这就是为什么我想使用ng-pluralize

我的问题是;您可以使用ng-pluralize 来执行此操作,而不必为此将元素放入另一个元素中吗?

【问题讨论】:

    标签: angularjs pluralize angularjs-ng-pluralize


    【解决方案1】:

    在同一元素中使用 ng-hide 和 ng-pluralize 的组合

    var module = angular.module('myApp', []);
    module.controller('mainController', function($scope) {
      //$scope.personCount = 0;
      $scope.personCount = 1;
      //$scope.personCount = 2;
    });
    .myEl {
      background-color: grey;
      width: 100%;
      height: 25px;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    <div ng-app="myApp" ng-controller="mainController">
      <ng-pluralize ng-if="personCount > 0" class="myEl" count="personCount" when="{'0': 'Nobody is viewing.',
                         'one': '1 person is viewing.',
                         'other': '{} people are viewing.'}">
      </ng-pluralize>
    </div>

    【讨论】:

    • 感谢您的精彩回答!
    最近更新 更多