过滤器中的Angular $解析

时间:2015-12-12 08:41:22

标签: angularjs angular-filters

我喜欢控制器中的这个,

$scope.types = Array({'name': '...', 'condition: 'type=1'}, {'name': '...', 'type=2 or type>4'});
$scope.items = Array({'id': 1, 'name': 'A', 'type': 1 }, {'id': 8, 'name': 'B', 'type': 2 }, {'id': 123, 'name': 'C', 'type': 2 });

我还有ng-repeat循环遍历$scope.items,以及自定义过滤器(使用| customFilter管道传输)。 customFilter可以访问所选的$scope.types并迭代$scope.items中的所有项目。

我想要做的是将字符串作为条件应用,以便能够确定是否过滤此项目,如下所示:

.filter('customFilter', function($parse) {

  return function(items, scope) {
    var filtered = [];
    if (_.isUndefined(scope.vm.type)) { // no need to filter
      return items;
    }
    var chkType = scope.vm.type.condition;
    angular.forEach(items, function(item) {
 //     var model = $parse(scope.vm.type.condition);
      var model = $parse("vm.type.condition");
      if (item.type === scope.vm.type[0]) {
        filtered.push(item);
      }
    });
    return filtered;
  } 
})

有人可以在这里帮助我,让它在过滤器内的循环内工作,或者是否有完全不同的方法?谢谢!

0 个答案:

没有答案