Angular.js - 通过嵌套属性过滤对象

时间:2015-06-30 00:03:05

标签: angularjs angularjs-directive angularjs-filter

我正在寻求建议。我的以下代码不起作用,很可能是因为$scope.clients对象更改时指令或过滤器无法更新:

$scope.clients = {
    "IDa": {"position": {index: 1}, "name": "a"},
    "IDb": {"position": {index: 2}, "name": "b"},
    "IDc": {"position": {index: 3}, "name": 'c'},
    "IDd": {"position": {index: 4}, "name": "d"},
    "IDe": {"position": {index: 5}, "name": "e"},
    "IDf": {"position": {index: 6}, "name": "f"},
    "IDg": {"position": {index: 7}, "name": "g"},
    "IDh": {"position": {index: 8}, "name": "h"},
    "IDi": {"position": {index: 9}, "name": "i"}
};

我需要根据position属性显示7个项目(div)。棘手的是,如果对象中的项目少于7个,则视图必须仍然只有7个项目,而不是名称属性,它会说“等待客户端”#39;视图必须按位置1..7的顺序显示这7个项目。

HTML:

<div class="streams__unit" stream client="clients | position:1">
       {{ client.name || 'Waiting for client' }}
</div>
<div class="streams__unit" stream client="clients | position:2">
       {{ client.name || 'Waiting for client' }}
</div>
<div class="streams__unit" stream client="clients | position:3">
       {{ client.name || 'Waiting for client' }}
</div>
<div class="streams__unit" stream client="clients | position:4">
       {{ client.name || 'Waiting for client' }}
</div>
<div class="streams__unit" stream client="clients | position:5">
       {{ client.name || 'Waiting for client' }}
</div>
<div class="streams__unit" stream client="clients | position:6">
       {{ client.name || 'Waiting for client' }}
</div>
<div class="streams__unit" stream client="clients | position:7">
       {{ client.name || 'Waiting for client' }}
</div>

这里是位置过滤器:

angular.module('app').filter('position', function () {
    return function (clients, index) {
        var client = {};

        for(var i in clients) {
            if(clients[i].position.index === index) {
                client = value;
                break;
            }
        };

        return client;
    }
});

但它不起作用,因为客户端对象的更新晚于过滤器触发(我认为)。这是指令:

angular.module('app').directive('stream', function () {
    return {
        restrict: 'EA',
        controller: function($scope) {
            $scope.$watch('client', function(val) {
                console.log('watch worked', val);
            });
        },
        scope: {},
        link: function (scope, element, attrs, fn) {
            scope.$watch(attrs.client, function(client) {
                scope.client = client;
            });
        }
    };
});

1 个答案:

答案 0 :(得分:1)

一种方法是两步法:

  1. 将对象预处理为理想的数组。
  2. 使用ng-repeat和&#39; orderBy&#39;过滤和限制到&#39;已应用过滤器。
  3. 这是一个有关的例子: http://plnkr.co/edit/AYEvW9DmxdZEF9lVSD20?p=preview

    HTML:

    <div ng-repeat="item in clientsArr | orderBy: 'position.index' | limitTo: 7" class="streams__unit">
       {{ item.name || 'Waiting for client' }}
    </div>
    

    JavaScript的:

    angular.module('app', []).controller('ExampleCtrl', function($scope) {
    
    $scope.clients = {
      "IDa": {"position": {index: 1}, "name": "a"},
      "IDb": {"position": {index: 2}, "name": "b"},
      "IDc": {"position": {index: 3}, "name": 'c'},
      "IDd": {"position": {index: 4}, "name": "d"},
      "IDe": {"position": {index: 5}, "name": "e"}
    };
    
    $scope.clientsArr = [];
    for(var i in $scope.clients) {
      $scope.clientsArr.push({
          id: i,
          position: $scope.clients[i].position,
          name: $scope.clients[i].name
        });
      }
    
      while($scope.clientsArr.length < 7) {
        $scope.clientsArr.push({});
      }
    });