使用布尔属性使用“track by”过滤Angular ng-repeat

时间:2016-11-23 07:46:58

标签: javascript angularjs angularjs-ng-repeat

$scope.users = [{
  "firstname": "Steve",
  "lastname": "Jobs",
  "id": 1,
  "userLog": {
    "id": 1,
    "userId": "Steve01",
    "enabled": true
  }
}, {
  "firstname": "Michelle",
  "lastname": "Jobs",
  "id": 2,
  "userLog": {
    "id": 2,
    "userId": "Michelle02",
    "enabled": false
  }
}];
<div ng-repeat="user in users|  filter: { enabled: true } track by user.userLog.id ">

帮助我如何过滤如果启用是真的,用户详细信息将显示其他隐藏。

由于

3 个答案:

答案 0 :(得分:2)

<div ng-repeat="user in users|  filter: { userLog.enabled: true } track by user.userLog.id ">

答案 1 :(得分:0)

<强>的js

<div ng-repeat="user in users | filter: {userLog :{enabled:true}}">
    {{user.firstname}}
</div>

示例

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
  $scope.users = [{
    "firstname": "Steve",
    "lastname": "Jobs",
    "id": 1,
    "userLog": {
      "id": 1,
      "userId": "Steve01",
      "enabled": true
    }
  }, {
    "firstname": "Michelle",
    "lastname": "Jobs",
    "id": 2,
    "userLog": {
      "id": 2,
      "userId": "Michelle02",
      "enabled": false
    }
  }];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">

  <div ng-repeat="user in users | filter: {userLog :{enabled:true}}">
    {{user.firstname}}
  </div>
</div>

答案 2 :(得分:0)

试试这个:

<div ng-repeat="user in users |  filter: (!user.userLog.enabled || '') track by user.userLog.id">