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
Filter in object
$scope.fmyData = $filter('filter')($scope.AllMyData, $scope.filterOptions,function (actual, expected) {
return actual.toLowerCase().indexOf(expected.toLowerCase()) == 0;
});
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.
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