angularjs:通过过滤阵列指令 [英] angularjs: passing filtered array to directive

查看:114
本文介绍了angularjs:通过过滤阵列指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方式来传递一个过滤数组指令:

I'm looking for a way to pass a filtered array to a directive:

我试过以下内容:

<my-directive model="myArray | filter:{myProperty: 'some value' }" /> 

但是,这并不工作。我认为它的意思与NG-重复使用,因为在这里我只是传递一个函数,而不是过滤的数组。

but that does not work. I think it's meant to be used with ng-repeat, because here I'm just passing a function instead of the filtered array.

有没有办法做到这一点,不是让我的数组的筛选副本等?

Is there a way to do that, other than making a filtered copy of my array ?

修改

下面是完整的code:

Here's the full code:

<request-service type="editing" jobs="vm.selectedMaterial.jobs | filter:{service.code: 'ED'}"></request-service>
<request-service type="translation" jobs="vm.selectedMaterial.jobs | filter:{service.code: 'TR'}"></request-service>

和指令:

(function () {
'use strict';

var directiveId = 'requestService';

angular.module('comp.domain.directives').directive(directiveId,  [directiveFunc]);

function directiveFunc(dependency) {
    return {
        restrict: 'E',
        templateUrl: 'app/dm/views/templates/requestService.html',
        scope: {
            type: '@',
            jobs: '='
        },
        link: function (scope, element, attrs) {                
        }
    };
}
})();

这样做的时候,我得到的错误转换圆形结构,以JSON

when doing so, I get the error 'Converting circular structure to JSON'

编辑2

以下建议的解决方案,我这样做:

Following suggested solution, I did that:

 $scope.filterJob = function (type) {
        if ($scope.vm.selectedMaterial) {
            return $scope.vm.selectedMaterial.jobs.filter(function (job) { return job.service.code === type; });
        };
    }

和视图:

  <request-service type="ED" jobs="filterJob('ED')"></request-service>

但是,这仍然给了我同样的错误。

But that still gives me the same error.

推荐答案

您两个问题是相互关联的。

Your both questions are related to each other.

您可以应用在NG-模型过滤器,但你不应该这样做。因为它会给你错误你面对你的第二个问题。

You can apply filter on on ng-model, but you should not do that. Because it will give you error you are facing in your second question.

在低通滤波器的工作,你的指令,棱角将会把手表上的工作变量。因此,当作业指令手表被分配被调用,这将再次触发过滤器,这将继续直到到达角最大消化周期。

When you pass filter job to your directive, angular will place a watch on your jobs variable. So when jobs gets assigned in directive watch get called, which will trigger filter again, and this will goes on until maximum digest cycle reached by angular.

要避免这种情况,你可以创建一个过滤器的方法,并通过在NG-模型方法。这样你就可以同时避免副本创建和最大消化周期错误。

To avoid this situation, you can create a filter method and pass that method in ng-model. This way you can avoid both copy creation and maximum digest cycle error.

这篇关于angularjs:通过过滤阵列指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆