Angular JS 'Startswith' custom filter

前端 未结 3 1133
滥情空心
滥情空心 2020-12-10 12:49

So I\'ve been trying a while to make a custom filter that searches for the \'Startswith\' parameters rather than the \'Contains\'. Every filter that I\'ve written haven\'t s

相关标签:
3条回答
  • 2020-12-10 13:04

    Filter in object

      $scope.fmyData = $filter('filter')($scope.AllMyData, $scope.filterOptions,function (actual, expected) {
                        return actual.toLowerCase().indexOf(expected.toLowerCase()) == 0;
                    });
    
    0 讨论(0)
  • 2020-12-10 13:27

    A simple way to do this is to add a new method to your scope.

    $scope.startsWith = function (actual, expected) {
        var lowerStr = (actual + "").toLowerCase();
        return lowerStr.indexOf(expected.toLowerCase()) === 0;
    }
    

    Then change the filter syntax on your element.

    <ul border="1px" ng-repeat="msg in messages | filter:search:startsWith">
    

    Here is a working plunkr with the example above.

    0 讨论(0)
  • 2020-12-10 13:28

    Now pure javascript itself has the startsWith() method on strings. But it will not be supported by IE11.

    See here:

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith

    0 讨论(0)
提交回复
热议问题