Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何按变量筛选AngularJS中的列表_Javascript_Angularjs_Angularjs Scope_Angular Promise_Angularjs Filter - Fatal编程技术网

Javascript 如何按变量筛选AngularJS中的列表

Javascript 如何按变量筛选AngularJS中的列表,javascript,angularjs,angularjs-scope,angular-promise,angularjs-filter,Javascript,Angularjs,Angularjs Scope,Angular Promise,Angularjs Filter,我有一个非常简单的任务。我想根据给定参数的角度对下拉菜单进行排序。应该很容易 我调用了一个dataservice,返回如下数据: "success" : true, "data" : { "this_status" : [{ "DefNo" : "111*A", "Com" : "111", }, { "DefNo" : "222*B", "Co

我有一个非常简单的任务。我想根据给定参数的角度对下拉菜单进行排序。应该很容易

我调用了一个dataservice,返回如下数据:

"success" : true,
    "data" : {
        "this_status" : [{
                "DefNo" : "111*A",
                "Com" : "111",
}, {
                "DefNo" : "222*B",
                "Com" : "222",
}, {
                "DefNo" : "333*C",
                "Com" : "333",
}
];
        "this_info" : [{
                 "Req" : "MUST",
                 "DefCom" : "111",
}, {
                 "Req" : "NoMUST",
                 "DefCom" : "222"
}, {
                 "Req" : "MUST",
                 "DefCom" : "333"
}]}
我的任务是列出所有DefCom值,这些值也有一个关联的MUST值。我需要在下拉列表中列出“DefCom”编号,其中“Req”为“必须”。在我的例子中,我的下拉列表的值是111和333

在我的控制器中,我执行调用

   $scope.getDefCom = function(){
        MyAPIservice.getDefCom().success(function(response){
            $scope.info = response.data.this_info;
            $scope.infoList = $scope.info;
        });
        }
我拥有这家工厂的地方:

angular.module('MyAPI.services', [])
  .factory('MyAPIservice', function($http) {
    var MyAPI = {};
    MyAPI.getDefCom = function () {
        return $http({
            method: 'GET',
            url: '/thisis/mylink/'
        });
    };

    return invoiceheaderAPI;
});
这是我的第一个计划,下拉列表列出DefCom编号。但是,接下来我需要过滤它们

在本例中,
$scope.require=MUST

在我的模板中:

<option ng-repeat="option in DefComList" value="{{option.DefCom}}">{{option.DefCom}} </option>
然而,经过更多的阅读,我找不到任何可以在过滤器中插入$scope变量的地方。大多数建议倾向于编写自己的过滤器。我用angular.forEach做了这个。我的过滤器如下:

 $scope.filterDefs = function($scope) {
        var clrreturn = false;
        angular.forEach($scope.info, function(value, key) {
            if (value.DefCom == $scope.require)
                clrreturn = true;

        });
        return clrreturn;
    };

但是,在从$scope.getDefCom函数获取$scope.info的结果之前,我的自定义筛选器正在运行。这引发了一个
错误:在$scope.info准备就绪之前,undefined不是对象(计算'$scope.info')
。我知道这与承诺有关,所以我试着写下延期承诺。然而,这也不起作用,我感到沮丧,因为这似乎应该是一个非常简单的任务,我可能会让自己比应该的更难

我觉得你可能想得太多了;如果我了解您想要做什么,那么您在过滤器中需要做的就是:

ng-repeat="option in DefComList | filter: require : true"
请注意,如果搜索“MUST”也将匹配“noMUST”,则精确匹配为true

这是我为你准备的一把小提琴:

另外,如果您特别只想搜索Req属性,则可以使用对象而不是字符串进行筛选。这是另一把小提琴,展示了这一点:


我希望这有帮助。

我觉得你可能想得太多了;如果我了解您想要做什么,那么您在过滤器中需要做的就是:

ng-repeat="option in DefComList | filter: require : true"
请注意,如果搜索“MUST”也将匹配“noMUST”,则精确匹配为true

这是我为你准备的一把小提琴:

另外,如果您特别只想搜索Req属性,则可以使用对象而不是字符串进行筛选。这是另一把小提琴,展示了这一点:


我希望这有帮助。

谢谢!这很好用。我肯定是想得太多了,这比编写自定义过滤器容易多了。没问题,很乐意帮忙!使用
ng选项
您也可以这样做。谢谢!这很好用。我肯定是想得太多了,这比编写自定义过滤器容易多了。没问题,很乐意帮忙!使用
ng选项
您也可以这样做。