Searching for two fields of an object in Angular using ng-repeat

Let's imagine we have this array

$scope.myArr = [{
    name: 'Marc Rasmussen',
    phone: 239470192,
    title: 'It Dude',
    description: 'Hello my name is Marc i am testing the fact that i can search for two fields in an object'
}, {
    name: 'Louise',
    phone: 1234567890,
    title: 'HR Director',
    description: 'I am also testing'
}, {
    name: 'Leonardo',
    phone: 123499,
    title: 'Actor',
    description: 'I have never won an oscar'
}];

As you can see, we have these fields:

  • name
  • phone
  • title
  • description

To display these fields, we use ng-repeat

<div ng-repeat="obj in myArr">
    <div>
        name: {{obj.name}}
    </div> 
    <div>
        phone: {{obj.phone}}
    </div>
    <div>
        title: {{obj.title}}
    </div>
    <div>
       description: {{obj.description}}
    </div>
</div>

Now, what we want is a single text field that can search for results in both the name and description fields simultaneously.

Usually, you would use a variable like $scope.search.$, but this searches in all fields. Alternatively, you could create an input field for each field, but that doesn't achieve what we need.

So, the question is, how can we make one input search in two fields at the same time?

Here is a fiddle link to the above code:

Fiddle

Answer №1

Check out this helpful resource I found: Using AngularJS to Filter by Multiple Model Properties (in OR relationship)

This technique involves utilizing angular's filter feature and implementing a predicate function for each item in the array being repeated.

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

Is there a way to remove the jQuery .css() upon clicking a different button?

I am currently working on a jQuery switch where I want the clicked button to revert back to its original state when another button is clicked. Additionally, I want the 'OFF' button to be automatically clicked first when the page loads. Despite tr ...

"Select2 dropdown fails to function properly upon returning to the page using the browser's

I've encountered an issue while working on a search page with Select2 dropdowns. Everything functions smoothly upon the initial page load, however, problems arise when a user performs a search, hits the browser back button, and then revisits the page. ...

Generating arrays of arrays from a string

Hello everyone, I am currently learning and trying to figure out how to convert a string into an array. var myString = 'one,two\nthree,four\nfive\nsix,seven' What I aim to achieve is converting the string into an array of arrays ...

Encountered an issue while processing the firebase get request with the following error message: 'Unauthorized request in Angular'

Here are my rules: Utilizing a Firebase database Calling an API URL from this section https://i.stack.imgur.com/auAmd.png I am attempting to retrieve data from a Firebase API in an Angular application, but I keep encountering an 'Unauthorized reque ...

Tips for implementing a live filter on an HTML table using JavaScript without the need to refresh the webpage

I successfully implemented these table filtering codes using plain JavaScript that I found on W3schools. The code filters table data based on input text and includes a select dropdown for additional filtering options. However, I encountered some issues whe ...

The text field is unable to receive input by clicking a button, only registering values when physically typed into the field

I have three text fields with buttons to increment or decrement the value, and a final text field that displays the calculation of the values in each field. Everything is functioning correctly, however, the issue arises when I try to add values using the ...

How can you conceal the navigation and footer components on a 404 page using Next.js?

import Footer from "./Footer"; import Navigation from "./Navigation"; import { useRouter } from "next/router"; function CustomLayout({ children }) { const router = useRouter(); return ( <> {router.pathname ...

Create HTML elements based on the information in a JSON object

My goal is to create span elements for each word in my subtitle text, which is stored in a JSON object. Here is the JSON data I am working with: var sub_info = [ {'start': 3.92, 'end': 6.84, 'words ...

Issues with monitoring multi-touch gesture events using Hammer JS

Can anyone figure out why this code, which utilizes hammer.js for multi-touch gesture events, is not functioning as expected? This code closely resembles one of the examples on Hammer's documentation. Despite setting up swipe events on specific elem ...

Use jQuery to target an element by its class name and node index

I am trying to target a specific element with the class ".myclass" by its node index, but I keep encountering an error stating that the element has no "animate" function. Here is an example: <div class="myclass"></div> <div class="myclass" ...

Exploring the process of setting up a datasource for Select2 in October CMS using the integrated Ajax Framework

Looking to utilize the extensive AJAX framework provided by October CMS to populate a Select2 box with data. The process of using a remote dataset in Select2 involves the following steps: $(".js-data-example-ajax").select2({ ajax: { url: "https://a ...

What is the best way to eliminate the lower margin in the UI-Boostrap Angular Tab directive?

The ui.bootstrap.tabs directive, which can be found here, seems to have a default margin before displaying its content. https://i.stack.imgur.com/NECGA.png Here is the HTML snippet: <uib-tabset active="activeJustified" justified="false" id="tabSet" s ...

Angular App successfully submits authentication calls to MobileFirst Adapter multiple times

Setup: MFPF v7.0 Eclipse: Luna SR.2 (4.4.2) Windows 7 An unusual problem has arisen for me. I am currently utilizing adapter based authentication in a specific Angular project. The application successfully authenticates, but it keeps repetitively calli ...

Tips for effectively overlaying one container on top of another in a responsive manner

There are a pair of containers. The main container functions as the parent, while the child container acts as a tag to categorize the content. The objective is to position the tag container at the top right corner of the main content container, slightly of ...

Minimize data once more using various key criteria

In my current setup, I have created a view that counts reports for different cities. function (doc) { if(doc.type == "report") { emit(doc.city_name, null); } } Using the _count reduce function, I get the following values: {'key': &a ...

What methods can be used to avoid regular expressions when searching for documents in MongoDB?

I am using a simple regular expression-based search in MongoDB like this: router.get('/search', function (req, res, next) { var text = req.query.text; collection.find({text: new ReqExp(text, 'ig')}, function (err, result) { ...

Angular controller fails to fetch scope variable in subsequent view

I'm currently working on implementing a login feature that directs a validated user to a personalized HTML view that greets them with their name. The initial view is a login page. When the user clicks the "Log In" button, the ng-click function is sup ...

Creating impenetrable div elements with JavaScript or jQuery

I'm currently working on creating solid blocks using DIVs positioned side by side both horizontally and vertically. I've successfully achieved this when the divs have equal heights. However, an issue arises when a div has larger dimensions; it en ...

Exporting two functions in JavaScript

Currently utilizing React, Redux, and experimenting with Material-UI integration. The example codes provided by Redux and Material-UI libraries include an 'export' statement at the end. Redux: export default connect(mapStateToProps, actions)(my ...

JavaScript: The variable `scopes` is declared

I'm completely new to JavaScript. Can anyone help me understand why this code isn't working, and what changes I need to make to fix it? function getResults(keywords) { foo.foo = function() { var bar = foo.getSomeText; // ...