【问题标题】:how to create Custom filter angularjs javascript controller side?如何创建自定义过滤器 angularjs javascript 控制器端?
【发布时间】:2014-12-31 13:04:19
【问题描述】:

如何创建自定义过滤器 angularjs javascript 控制器端? 我想在称为 SegmentId 的数组中搜索,以创建过滤器,在 SegmentId 上搜索分段数组 -

    //Controller

    $scope.GetSegmentDetails = function (id) {
        $scope.SegmentName = $filter('SegmentById')($scope.segments,id);
    }


    //Filter
    app.filter('SegmentById', function () {
        return function (input, searchPerson) {
        if (!searchPerson)
            return input;

        var results = [];
        angular.forEach(input, function (person) {
        }
    });

    return results;
}

});

【问题讨论】:

  • 请发布一个示例模型(即$scope.segments
  • 我不明白您想创建一个过滤器来将逻辑放入控制器中?那么你不需要过滤器,你可以直接在控制器中进行,过滤器用于过滤在 HTML 中绑定的显示数据
  • @boindiil [{"SegmentId":"1","Description":"hod Registrations"}, {"SegmentId":"2","Description":"hod Inactive"}, { "SegmentId":"3","Description":"hod testUpd"}, {"SegmentId":"8","Description":"hod test"}, {"SegmentId":"1111","Description": "hod Release"}, {"SegmentId":"12","Description":"hod Requests"}, {"SegmentId":"13","Description":"hod Welcome Back"}]
  • @Charlie 是的,我知道我可以直接作为私有函数执行,还需要在页面上执行它自己,相同的功能...
  • 哦,好吧,您是否尝试在控制器中导入角度模块的过滤器对象?因为我不确定模块中的组件是否知道彼此隐含地不知道该怎么做,但你应该用 google 找到 smth

标签: javascript html angularjs angularjs-filter


【解决方案1】:

只需要在你的控制器中添加过滤器依赖然后调用它。

app.controller("YourController", ['$scope', '$filter', function ($scope, $filter){

$filter('filterName')($args));

}]);

【讨论】:

    【解决方案2】:

    您不必编写一个过滤器来过滤SegmentId。这里有一个例子

    function MyCtrl($scope, $filter) {
      $scope.data = [{"SegmentId":"1","Description":"hod Registrations"}, {"SegmentId":"2","Description":"hod Inactive"}, {"SegmentId":"3","Description":"hod testUpd"}, {"SegmentId":"8","Description":"hod test"}, {"SegmentId":"1111","Description":"hod Release"}, {"SegmentId":"12","Description":"hod Requests"}, {"SegmentId":"13","Description":"hod Welcome Back"}]
      
      $scope.filterData = function(segmentId) {
        return $filter('filter')($scope.data, { SegmentId: segmentId });
      }
    }
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    <div ng-app ng-controller="MyCtrl">
      Full:
      <ul>
        <li ng-repeat="Segment in data">
          {{Segment.SegmentId}}
        </li>
      </ul>
      Filtered in View:
      <ul>
        <li ng-repeat="Segment in data | filter:{SegmentId:1111}">
          {{Segment.SegmentId}}
        </li>
      </ul>
      Filtered in Controller:
      <ul>
        <li ng-repeat="Segment in filterData(1111)">
          {{Segment.SegmentId}}
        </li>
      </ul>
    </div>

    【讨论】:

    • 非常感谢!工作得很好我有另一个关于将模块加载到控制器的顺序的问题。这意味着 $filter 假设在 $scope 之后是第二个
    • 看看this link
    • @boindiil 如何使用 $filter 实现SegmentId !== segmentId
    • @gauravbhavsar 你可以使用negation within filter
    • @boindiil (+1) 谢谢 :)
    猜你喜欢
    • 1970-01-01
    • 2016-10-14
    • 1970-01-01
    • 2014-12-01
    • 2013-11-29
    • 2014-10-22
    • 2019-04-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多