AngularJS ng-repeat filter fails to function with changing field names dynamically

Here is a code snippet I am working with:

<input type="text" ng-model="filteredText">
<ul>
    <li ng-repeat="item in data | filter: {Name : filteredText}">

    </li>
</ul>

Initially, when the Name property is static, everything functions correctly. However, I now require the ability to filter by a dynamic field. For example:

<ul>
    <li ng-repeat="item in data | filter: {propertyName: filteredText}">

    </li>
</ul>

In this scenario, the propertyName is dynamic and is sourced from the $scope. Unfortunately, using this dynamic value in the filter directive does not yield the desired result.

Answer №1

Upon carefully reviewing the documentation, it appears that an alternative approach must be taken.

<input ng-show="propertyFilter == 'Name'" 
       type="text"
       ng-model="filteredText[propertyName]" />

<ul>
    <li ng-repeat="item in data | filter: filteredText">

    </li>
</ul>

Answer №2

Here is a possible solution that you can use: sample code

<div ng-controller="MyCtrl">
   <div ng-repeat="line in lines | filter:custom()">
      <p>
      {{line.Title}}
      </p>
    </div>
</div>

var myApp = angular.module('myApp', [])
function MyCtrl($scope) {
    $scope.filterText = 'object';
    $scope.fieldName = 'Title'

    $scope.custom = function(){
      var obj = {};
      obj[$scope.fieldName] = $scope.filterText;

        return obj;
    }

    $scope.lines = [
        {Title: 'First Item', value: 20},
      {Title: 'Second Item', value: 23}
    ];
}

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

Bringing in an external .js file into nuxt.config.js

Having trouble importing config.js with the project's API keys and getting undefined as a return value. //config.js var config = { fbAPI: "key" } - //nuxt.config.js const cfg = require('./config') env: { fbAPI: cfg.apiKey } Wonder ...

Incorporating CSS Styles in EJS

Struggling with connecting my CSS files while using EJS. I've checked out another solution but still can't seem to get it right. Here is the code I used as a reference for my CSS file. EJS <html> <head> <meta charset="utf-8 ...

Tips for utilizing the useContext hook in Next.js

I'm facing an issue with persisting information between different pages using nextjs and the useContext hook. Despite changing a variable in one page, it reverts back to its default value when navigating to another page. Below is a glimpse of the dir ...

Attempting to dynamically update the image source from an array when a click event occurs in a React component

Has anyone successfully implemented a function in react.js to change the image source based on the direction of an arrow click? For instance, I have an array set up where clicking the right arrow should move to the next image and clicking the left arrow s ...

How to reference a nested closure function in Javascript without relying on the 'this' keyword

I'm facing a situation where I have a tree of closures: 'A' contains private closures 'pancake' and 'B'. There are times when I need to call the private closure 'pancake' from inside 'B' and access its ...

Improving observables in Angular after triggering a resolver

After successfully creating a Resolver in my code, I am wondering if there is a way to refactor my TypeScript component. Currently, whenever I try to remove any unnecessary parts of the code, it breaks my app. Here is the code for my resolver: @Injectable( ...

The functionality of triggers is not applicable to content that is dynamically loaded through ajax requests

I encountered a problem recently that required me to rewrite some functions. The original code looked like this: $('#type_of').on('change', function () { However, due to issues with manipulating the DOM and loading content via ajax, I ...

Steps for replacing $httpProvider in a service when there is already a defined $httpProvider

Having trouble with my factory service: app.factory('sessionInjector', ['sessionService', 'stateService', '$q', function (sessionService, stateService, $q) { var myInjectorInstance = { /* ... */ }; return m ...

What advantages can be gained by opting for more precise module imports?

As an illustration, consider a scenario where I have an Angular 6 application and need to bring in MatIconModule from the @angular/material library. Two options could be: import { MatIconModule } from '@angular/material/icon'; Or import { Mat ...

Storing newly generated rows using PHP and JavaScript

In my form, I have dynamically added rows that work properly. Once the user fills in the form fields and clicks on the "check correctness" submit button, validation is performed. If the data is incorrect, a correction is required. If the data is correct, a ...

Struggling with integrating Meteor.wrapAsync with the mailchimp-api-v3

I am currently exploring the use of mailchimp-api-v3 in a Meteor project (1.4.1.3) and I particularly like the batch support it offers. To make the call, I have enclosed it within Meteor's .wrapAsync function (which had a bit of a learning curve, but ...

Rewriting a JSON tree structure in NodeJS

Exploring a new method in comparison to my previous post. The situation involves a JSON structure as follows: var data = { "tree": { "id": "99842", "label": "Bill", "children": [ { "id": "27878", ...

Pass PHP date to JavaScript and increase its value

I'm looking to retrieve the server time with PHP and store it in a JavaScript variable. Once stored, I'd like it to continuously increment. Below is the code snippet: function initTime(date){ var today=new Date(date); var h=today.getHours(); v ...

Navigating through pages using Jquery's show and hide functionality

How come my item is not returning? I used the .show() method on the item. <div id="back">< back</div> <div class="item">item</div> <div class="content">My content</div> $(function(){ $('.item').on(&apo ...

Revolutionizing the Industry: Discover the Power of Dynamic Models, Stores, and Views for

As a newcomer to EXT JS, I am exploring the use of MVC in my projects. Consider a scenario where I have a web service with a flexible data model. I would like to dynamically create models, stores, and components based on the data from these stores. Let&a ...

How can you make a checkbox function like a select in AngularJS?

Is it possible for AngularJS to automatically bind all values (options) when a user selects something from a list, populating them via the model? How could I achieve a similar functionality with checkboxes? I want a select for multiple values and a checkbo ...

Adjust ThreeJS camera zoom to match the size of a specific HTML element

I am currently diving into the world of Three.js and have stumbled upon an issue while trying to incorporate a plane geometry based on an image tag from the DOM. Situation Here is my current HTML template : <figure> <div class="aspect&quo ...

Adding a stylesheet dynamically to the <head> tag using $routeProvider in AngularJS

Is there a way to load a specific CSS file only when a user visits the contact.html view on my AngularJS application or site? I came across this helpful answer that almost made sense to me How to include view/partial specific styling in AngularJS. The acce ...

Create a new object in Three.js every x seconds and continuously move each object forward in the Z-axis direction

I am currently developing a Three.js endless runner game where the player controls a character dodging cars on a moving road. At this early stage of development, my main challenge is to make the hero character appear to be moving forward while creating the ...

Transform JSON array containing identical key-value pairs

My array is structured as follows: [ { "time": "2017-09-14 02:44 AM", "artist": "Sam", "message": "message 1", "days": 0 }, { "time": "2017-09-14 02:44 AM", " ...