The Angular filter received an undefined value as the second parameter

Currently, I am facing an issue while trying to set up a search feature with a custom filter. It appears that the second parameter being sent to the filter is coming through as undefined. The objects being searched in this scenario are books, each with a genre array attribute.

To aid in the searching process, I have an array of genre objects that are populated via the controller in a multiple select element.

The problem lies in the fact that the second parameter being passed to the custom filter is showing up as undefined. Below is the code snippet for the filter located within the controller. Upon debugging, I've confirmed that the filter logic itself is correct, except for the fact that it's indicating 'filterBy' as undefined:

CONTROLLER -

app.filter('genres', function () {
    return function (books, filterBy) {
        console.log(filterBy);
        return books.filter(function (element, index, array) {
            return element.genre === filterBy.genre;
        });
    };
});

HTML-

<div ng-controller="LibraryController as libraryCtrl">
    <form>
        Book name
        <input type="text" name="Book name" ng-model="filterBy.name" class="inputTxtForm">
        <br/> Author
        <input type="text" name="author" ng-model="filterBy.rating" class="inputTxtForm">
        <br/> Genres
        <select multiple="true" ng-model="filterBy.genre" ng-options="genre for genre in libraryCtrl.genres" class="inputTxtForm">
        </select>
    </form>
    <section class="tableSection">
        <table>
            <tr ng-repeat="book in libraryCtrl.books | filter:filterBy | genres:filterBy">
                <td>
                    {{book.name}}
                </td>
                <td>
                    {{book.author}}
                </td>
                <td>
                    {{book.genre.join()}}
                </td>
            </tr>
        </table>
    </section>
</div>

Any ideas on what could be going wrong here?

Answer №1

Update filterBy to libraryCtrl.filterBy

<div ng-controller="LibraryController as libraryCtrl">
<form>
    Book title
    <input type="text" name="Book title" ng-model="libraryCtrl.filterBy.title" class="inputTxtForm">
    <br/> Author
    <input type="text" name="author" ng-model="libraryCtrl.filterBy.rating" class="inputTxtForm">
    <br/> Genres
    <select multiple="true" ng-model="libraryCtrl.filterBy.genre" ng-options="genre for genre in libraryCtrl.genres" class="inputTxtForm">
    </select>
</form>
<section class="tableSection">
    <table>
        <tr ng-repeat="book in libraryCtrl.books | filter:libraryCtrl.filterBy | genres:libraryCtrl.filterBy">
            <td>
                {{book.title}}
            </td>
            <td>
                {{book.author}}
            </td>
            <td>
                {{book.genre.join()}}
            </td>
        </tr>
    </table>
</section>

View the example [link] http://plnkr.co/edit/FtWDzTvxROECsOs0zTQ7?p=preview

Check the console log for filterby values

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Click event in dropdown selection options

Is it possible to use ng-click in select options for different functions to be triggered on each option selection? Other threads have suggested using the same controller function for all options, but I am looking to trigger different functions based on t ...

The Ionic framework has a defined variable

In my code, I have initialized a variable inside the constructor like this: constructor(public http: HttpClient) { this.data = null; this.http.get(this.url).subscribe((datas: any) => { this.dbUrl = datas[0].db_url2; console.log(this ...

Tips on passing methods to form provider with unique name for managing nested forms

As discussed in #60277873, when creating nested forms, it is necessary to rename the methods of the nested form as follows: const { register, formState: { errors }, handleSubmit, } = useForm({ mode: "onBlur", }); This code sh ...

What should I designate as the selector when customizing dialog boxes?

I am attempting to change the title bar color of a dialog box in CSS, but I am running into issues. Below is the HTML code for the dialog box and the corresponding CSS. <div id="picture1Dialog" title = "Title"> <p id="picture1Text"> ...

Create a duplicate of the table and remove specific rows

Hi there, I have a query about duplicating a table. I am looking to extract specific results and display only those results. To illustrate, here is an example in HTML code: <table class="table table-striped" id="ProfileList2"> <tbody> ...

The functionality for the angular ng-model-options getterSetter is not functioning as expected

I've been trying to utilize the ng-model-options getterSetter feature, but I'm having trouble getting it to execute my model function. <input id="idShowDisabled2" type="checkbox" ng-model="myObj.Trev" ng-model-options="{ getterSetter: true }" ...

When attempting to call a Firebase Cloud Function URL in an AngularJS $http request, an Access Control Origin Error

I recently created a cloud function that involves linking with Plaid. I'm currently working on calling this function using AngularJS's $http method. While the cloud function code is being executed, I encountered an error in my console instead of ...

Relocating to reveal or conceal item

I am currently working with jQuery and trying to achieve a specific functionality. My goal is to hide and unhide an element, while also focusing on the exposed area once it becomes visible. I have a link #welcomeselect that, when clicked, should reveal t ...

`Look up values from specified key names`

Consider the following JSON data: const information = { "item1":1, "item2":20, "item3":123, "item4":[{"a":"apple","b":"ball"}], "item5":1211 } In ...

With Ionic, you can use one single codebase for both iPad and iPhone

I currently have a complete app developed using ionic and angularjs that is functioning well on iPads and Android devices. Now we are looking to launch it for iPhones and Android smartphones with some design modifications. Is there a method to achieve th ...

Unlock the Power of Core 2 MVC with the Cutting-edge Integration of

Looking for a solution on how to effectively use JQuery Datatables with Core MVC? Check out this helpful resource: Using jQuery DataTables Grid With ASP.NET CORE MVC I recently downloaded the sample project and made some modifications to fit my needs. It ...

Is there a way to execute a Node 6 npm package within a Node 5.6.0 environment?

I am currently utilizing a tool called easy-sauce to conduct cross-browser JavaScript tests. Essentially, my package.json file references this tool for the test command: { "scripts": { "test": "easy-sauce" } } Everything runs smoothly when I exec ...

What is the best way to reference class variables and methods within a callback function in Typescript?

While working on my Angular project with the Highcharts API, I encountered a situation where I needed to pass a state code to a class level method after drilling down to a specific map location. Below is the snippet of my current code: ngOnInit() { this. ...

How can I trigger a PHP function by clicking a button on a PHP page that has already been loaded?

While I've come across a variety of examples, I haven't been able to make them work for the simple task I need to accomplish. The code in these examples seems overly complex compared to what I require. In essence, I have a form that processes dat ...

Troubleshooting in Electron: What is the best way to access objects within the render scope from the console?

During my experience in web development, I have always appreciated the ability to access and manipulate variables and functions through the browser's development console at runtime. For instance, if I define a var foo = 3; in my code, I am able to ...

Troubleshooting the issue: Incompatibility between jQuery .focus() and dynamically generated list items from ng-repeat in AngularJS

I am currently working on enhancing the keyboard accessibility of a website, specifically focusing on making a dropdown menu accessible via keyboard. I am attempting to establish focus on the element with the id= list-0. Below is the HTML code snippet: & ...

Displaying a two-dimensional array from a JSON file using AngularJS ng-repeat

Looking at this HTML file, I am trying to display a 2D array from the json-$scope.listOfIngredient <div class="ingredientMapping" ng-repeat="IngredientMapping in listOfIngredient track by $index"> <ul> <!-- BEGIN: Inner ngRep ...

Include CakePHP named parameters in the URL when selecting from a list

If I have a selection list like the one below: <select name='languages'> <option value='german'>German</option> <option value='english'>English</option> </select> How can I use Jav ...

Using React Router can sometimes lead to an issue where the React components are

My server side rendering is set up for performance, but I am encountering discrepancies between the client and server renderings. The client initially renders <!-- react-empty: 1 --> instead of components, which leads to a different checksum. As a re ...

Blip Scripts: Converting JSON to JavaScript - Dealing with Undefined Arrays

I am currently working on a project to develop a bot on Blip. There are certain parts where I need to send a request to an API and then use a JavaScript script to extract elements from the JSON response. The JSON response I received from the API is stored ...