angularjs避免ngrepeat的空场

时间:2014-12-03 18:18:02

标签: javascript arrays json angularjs

删除| currency:"£"过滤器时,该功能似乎没问题。是否有任何选项只显示当前用户数量?



(function() {
  var app = angular.module('myApp', []);
  app.controller('roomController', function($scope) {
    $scope.users = [{
      id: 1,
      firstname: 'member',
      spent: 206.98
    }, {
      id: 2,
      firstname: 'member',
      spent: 12.23
    }, {
      id: 3,
      firstname: 'member',
      spent: 302.12
    }, {
      id: 4,
      firstname: 'member',
      spent: 198.21
    }, {
      id: 5,
      firstname: 'member',
      spent: 85.85
    }];
    $scope.currentuser = 4;
  });
})();

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
  <div ng-controller="roomController">
    <h2 ng-repeat="user in users"><i class="fa fa-gbp"></i> {{ user.id === currentuser ? user.spent: '' | currency:"£"}}</h2>
  </div>
</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

如果我理解正确,您不需要三元运算符,而是将ng-if放在正在重复的<h2>中。

见下文:

(function() {
  var app = angular.module('myApp', []);
  app.controller('roomController', function($scope) {
    $scope.users = [{
      id: 1,
      firstname: 'member',
      spent: 206.98
    }, {
      id: 2,
      firstname: 'member',
      spent: 12.23
    }, {
      id: 3,
      firstname: 'member',
      spent: 302.12
    }, {
      id: 4,
      firstname: 'member',
      spent: 198.21
    }, {
      id: 5,
      firstname: 'member',
      spent: 85.85
    }];
    $scope.currentuser = 4;
  });
})();
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
  <div ng-controller="roomController">
    <h2 ng-repeat="user in users" ng-if="user.id === currentuser"><i class="fa fa-gbp"></i> {{ user.spent | currency:"£"}}</h2>
  </div>
</div>