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

Developing a Customized Filtering Mechanism in Angular 8

I have some experience working in web development, but I am relatively new to Angular. My current project involves creating a simple filter for a table's column based on user input. However, I'm facing an issue where typing in a single letter fil ...

The current layout of the div is hindering the ability to switch from vertical scrolling to horizontal scrolling

I'm currently utilizing this scroll converter tool to transform vertical scrolling into horizontal scrolling. However, despite correct script inclusion and initialization, the conversion is not working as expected. It seems like there might be an issu ...

How can one utilize JSON.parse directly within an HTML file in a Typescript/Angular environment, or alternatively, how to access JSON fields

Unable to find the answer I was looking for, I have decided to pose this question. In order to prevent duplicates in a map, I had to stringify the map key. However, I now need to extract and style the key's fields in an HTML file. Is there a solution ...

Using jQuery to emphasize search text in a user-friendly manner

After successfully implementing a search function for a table, I faced the challenge of highlighting the search keyword within a cell. Despite searching for a solution online, I couldn't find anything useful! Here is my HTML code: <table class="s ...

Utilizing JQuery for parsing information

I am working on a project that involves displaying Google Maps API on one tab and images on another within a container. The goal is to have the image in the second tab change when a location is selected from the list. To achieve this, I have set up a hidd ...

Showing the information obtained from an API request once the user submits the form without the need to reload the page

I am currently working on a form that will take search query requests from users upon submission and then display the results by making an API call. My goal is to show these results without the page having to refresh, using AJAX. The backend connection to ...

Autocomplete feature integrated within search bar

I'm currently experimenting with merging MUI autocomplete and MUI searchbar to create a Searchbar that provides suggestions. I have attempted the following: https://codesandbox.io/s/material-demo-forked-cthpv import React from "react"; impo ...

Updating the table row by extracting data and populating it into a form

As part of my project, I am implementing a feature where I can input 'Actors' into a table using a Form. This functionality allows me to select a row in the table and retrieve the data of the chosen Actor back into the form for updating or deleti ...

Submitting multiple forms using AJAX and PHP

I am currently trying to send the form data to another page for processing, but I am not receiving any data. Below are the forms in question: The default form is the login form for requesting a username and password, with only one submit button. ...

Activate the function within the same class only for the selected item

Hello there, I'm struggling to grasp how to make this particular functionality work. Basically, I have 8 divs, each containing an image used as a button with the same class (tm-img), and hidden divs with additional information. My goal is to initiall ...

Display JSON array as a table using JavaScript

Looking for assistance with integrating an API that queries the Instagram API. I want to consume my API and display the Tags Search results in a table format. The JSON response structure is as follows: {"data":[{"media_count":13485788,"name":"argentina"}, ...

ASP not recognizing AngularJS input states

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="Staffing_Tool.Login" %> <!DOCTYPE html> <html> <head runat="server"> <script src="scripts/angular.min.js" type="text/javascript"></scr ...

What is the best way to incorporate a loading icon onto a webpage that exclusively runs JavaScript functions?

I frequently use Ajax load icons to indicate progress during ajax requests. Is there a way to achieve the same result using regular JavaScript? For instance: $('button').on('click', function(){ showLoadingIcon(); lengthyProces ...

The Failure of window.pushState in HTML 5 History

Looking to implement ajax loading for updating center content and URL without refreshing the page. Everything seems to be working fine except for the history management. It appears that window.pushState is not properly recording URLs or the popstate even ...

Using Angular 6's httpClient to securely post data with credentials

I am currently working with a piece of code that is responsible for posting data in order to create a new data record. This code resides within a service: Take a look at the snippet below: import { Injectable } from '@angular/core'; import { H ...

Is there a way to use node.js to retrieve a video in mp4 format?

My goal is to allow users to download a video from my AWS S3 bucket in MP4 format: app.get("/download_video", function(req,res) { filename = "s3.xxx.amazon.com/bucketname/folder/video_example.mp4"; // I'm unsure about the next steps }); Whil ...

Using Node/Express to split the request headers with the .split() method

I am currently working on a way to determine if a specific item exists in the req.headers in order to make a decision on what to send back to the user. Here is my code snippet: function serveAppData(req, res) { console.log("CHECKME", req.headers); //var h ...

Restricting the amount of requests per user within a single hour [Node.js]

As I work on developing a server side application using Nodejs and Express, one thing that crosses my mind is limiting the number of requests per user within a specific time frame to prevent malicious hackers from overwhelming the server with spam. I&apos ...

Display just the minutes using react-countdown-circle-timer in a React application

Looking to create a test page in React featuring a countdown timer set at 50 minutes, I integrated the react-countdown-circle-timer module. However, I encountered an issue where the minutes displayed do not change dynamically as they should. My goal is to ...

using javascript to retrieve php variables

After creating a webpage, setting up Apache2 on an Ubuntu server to access it over the internet, and installing PHP5 and MySQL, I encountered issues with accessing database information on my page through a .php file. Despite having a file named test.php th ...