简体   繁体   English

AngularJS:过滤器数组

[英]AngularJS: filter array

I created an element directive for a table with data. 我为带有数据的表创建了一个元素指令。 The table uses ng-repeat s to render all of the rows/columns. 该表使用ng-repeat呈现所有行/列。

I created an attribute directive for the element directive. 我为element指令创建了一个属性指令。 It's purpose is to get the columns you do not wish to include in the table. 目的是获取您不希望包含在表中的列。 The full directive might look something like this: 完整指令可能如下所示:

<extended-table exclude="columnone, columntwo"></extended-table>

The table is interpreted as: 该表解释为:

<table>
    <tr>
        <th ng-repeat="column in columns">
        {{column}}
        </th>
    </tr>
    <tr ng-repeat="row in data | startFrom:currentRow">
        <td ng-repeat="column in columns">
            {{row[column]}}
        </td>
    </tr>
</table>

In the ng-repeat s, I would like it to ignore the values in exclude, but i'm a bit lost on where to go from here. ng-repeat ,我希望它忽略exclude中的值,但是我对从这里去哪里有些迷茫。

app.directive('exclude', function () {
    return function (scope, element, attrs) {

        var params = attrs.exclude.split(',');

        for (var i = 0; i < params.length; i++) {
            params[i] = params[i].trim();
        }

        scope.exclude = params;
    };
});

How would I make the ng-repeat ignore column both for the header and rows if the column is contained within the $scope.exclude array? 如果该列包含在$scope.exclude数组中,如何使ng-repeat忽略标题和行的列?

app.filter('startFrom', function () {
    return function (input, start) {
        start = +start;
        return input.slice(start);
    };
});

My recommendation would be to instead read the exclude attribute via your extended-table directive, rather than creating a custom directive to do it. 我的建议是改为通过extended-table指令读取exclude属性,而不是创建自定义指令来执行此操作。

Here is some incomplete code showing how this could be done: 这是一些不完整的代码,显示了如何完成此操作:

myModule.directive('extended-table', function() {
    return {
...
      scope: {
        'exclude': '@' // This says to load the attribute 'exclude' into a scope property of the same name.
      }
...
    // In your link functions you could process the scope.exclude property as you wish.
    };
  });

More information can be found at The Hitchhikers Guide to the Directive : 可以在The Hitchhikers Directive Guide中找到更多信息:

@ – binds the value of parent scope property (which always a string) to the local scope. So the value you want to pass in should be wrapped in {{}}. Remember `a` in braces.
= – binds parent scope property directly which will be evaluated before being passed in.
& – binds an expression or method which will be executed in the context of the scope it belongs.

The biggest advantage to this approach is that you are not creating two directives that are dependent on eachother. 这种方法的最大优点是您不会创建两个相互依赖的指令。

Note: 注意:

When using @ to bind, remember to pass your properties with {{}} notation: 使用@进行绑定时,请记住以{{}}表示法传递属性:

    <myDirective myDirectiveAttribute="{{someProperty}}"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM