Custom Angular filter never called?
NickName:Tiago Ask DateTime:2015-05-16T00:21:45

Custom Angular filter never called?

I have an array of strings which I'd like to filter by first letter using a custom angular filter.

I have the following ng-repeat set up:

<div ng-repeat="proverb in proverbs | firstLetter">
  <a href="#/{{langID}}/{{$index}}">{{proverb}}</a>
</div>

And this is my custom filter, currently using a default letter "A" for testing purposes:

angular
  .module('raidersApp', [
    'ngRoute',
    'ngTouch',
    'ngResource'
  ])
  .filter('firstLetter', function () {
    return function (input) {
      input = input || [];
      letter = "A";
      var out = [];
      input.forEach(function (item) {
        console.log("current item is", item, item.charAt(0));
        if (item.charAt(0).toUpperCase() == letter) {
           out.push(item);
        }
      });
      return out;
    };
  })

The problem is, this filter is currently filtering everything out (the app works and there are no console errors but the ng-repeat section is blank), and the console.log is never even being displayed.

What's missing?

Copyright Notice:Content Author:「Tiago」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/30264315/custom-angular-filter-never-called

Answers
dting 2015-05-15T20:35:33

You need to post more of your code. Your problem is most likely an issue with how you structured your app. \n\nHere is a similar filter that does what I think you are looking for:\n\n\r\n\r\nvar app = angular.module('raidersApp', [])\r\n .controller('myController', function($scope) {\r\n $scope.langID = \"en\";\r\n $scope.proverbs = ['Apple', 'Apricot', 'Banana', 'Orange'];\r\n })\r\n .filter('firstLetter', function() {\r\n return function(input, letter) {\r\n return (input || []).filter(function(item) {\r\n return item.charAt(0).toUpperCase() === letter;\r\n });\r\n };\r\n });\r\n<html ng-app=\"raidersApp\">\r\n\r\n<body>\r\n <script src=\"https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js\"></script>\r\n <div ng-controller='myController'>\r\n <h3>A</h3>\r\n <div ng-repeat=\"proverb in proverbs | firstLetter: 'A'\">\r\n <a href=\"#/{{langID}}/{{$index}}\">{{proverb}}</a>\r\n </div>\r\n <h3>O</h3>\r\n <div ng-repeat=\"proverb in proverbs | firstLetter: 'O'\">\r\n <a href=\"#/{{langID}}/{{$index}}\">{{proverb}}</a>\r\n </div>\r\n </div>\r\n</body>\r\n\r\n</html>",


More about “Custom Angular filter never called?” related questions

Custom Angular filter never called?

I have an array of strings which I'd like to filter by first letter using a custom angular filter. I have the following ng-repeat set up: &lt;div ng-repeat="proverb in proverbs | firstLetter"&gt;...

Show Detail

Angular: registerOnChange is never called while testing component with custom ControlValueAccessor

As I describe in this answer, I created a custom ControlValueAccessor directive to have control on when to fire the onChange event of my component, and everything works perfectly, except when I tes...

Show Detail

Jackson - Custom Deserializer is never called

I have a class that needs to be deserialized from a JSON response. Since the response contains a String value for one of the fields and I need it as an int, I implemented a custom deserializer The

Show Detail

Custom function never called

I was trying to implement a exclude button for the multi_select filter_type, using multi_select_custom_func. I also tried custom_funcp. The problem is the custom function is never called. I'm sure ...

Show Detail

Custom comparator for a single property for Angular $filter

First, my reason for creating the custom comparator in the first place is because an attribute on the expression that is an array--such as 'tags'--required the expression to have the values in a

Show Detail

Spring Security Custom Filter Never Called

The problem is that my custom Spring Security filter is never invoked when a request comes in. During debugging, I found that in doFilter() in FilterChainProxy, my filter (JwtAuthenticationFilter) ...

Show Detail

Why is `doesFilterPass` not being called in this custom filter

It's my understanding that when filterChangedCallback is called that the grid filters the current data and calls doesFilterPass and isFilterActive as it does so. Here I have column that uses a cus...

Show Detail

Deinit never called on custom UIView

Hello I have a UIViewController which has 4 custom UIView. Deinit function inside UIViewController is called when I change the rootViewController but doesn't called inside custom UIView. Therefore,...

Show Detail

Custom filter in Angular

I have some problem with custom filter. I can't include it's in my project. Firstly I used a filter: text. I understand that array initialized asynchronously and used this custom filter. But when i

Show Detail

AngularJS custom filter being called twice

I've created a custom filter using AngularJS that prints out the fruits that start with a p. As far as I can tell, I've implemented the custom filter correctly. I'm printing out a message every ti...

Show Detail