How can I filter in Angular using ng-click for values greater than a certain threshold?

I am looking to narrow down the rows where the "age" property is higher than a specific number, let's call it "x".

    <a href="#" ng-click="personFilter = {age:18}">Older than 18</a>

<div ng-repeat="person in persons | filter:personFilter ">
      <div>{{person.name}}</div>
      <div>{{person.age}}</div>
</div>

Your help is greatly appreciated.

Answer №1

Based on the information provided in the documentation, it is possible to pass a function to the filter method. You can see an example here.

<!DOCTYPE html>
<html>

  <head>
    <script data-require="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9dfcf3fae8f1fcefb3f7eeddacb3afb3a9">[email protected]</a>" data-semver="1.2.4" src="http://code.angularjs.org/1.2.4/angular.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>

  <body ng-app="example" ng-controller="Ex">
    <!-- When clicked, set the filter to our custom function defined in scope -->
    <a href="#" ng-click="personFilter = isOver18">Older than 18</a>
    <ul>
      <li ng-repeat="p in people | filter:personFilter">
        {{p.name}} - {{p.age}}
      </li>
    </ul>
  </body>

</html>

Controller:

angular.module('example', [])
  .controller('Ex', function($scope) {
     $scope.personFilter = {};
     $scope.people = [
        {name: "Bob", age: 32},
        {name: 'Billy', age: 12}
      ];

      /* Returns true if the provided person is over 18 */
      $scope.isOver18 = function(p) {
        return p.age > 18;
      }
  });

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

Having trouble retrieving the data from the second child object in an object returned by an API call in a

Having trouble accessing the second child object of an object generated by an API response. The weather API in question can be found at . Refer to Display.js for a detailed explanation. App.js import React,{useState} from 'react'; import Navbar ...

Load styles in Nuxt asynchronously to improve performance

Is there a way to load styles asynchronously on the website or insert them in the footer? I am currently using Nuxt version 2.0.0 Here's what I have tried so far: Adding a plugin in webpack called async-stylesheet-webpack-plugin. However, this res ...

From transitioning from AngularJS to the latest version Angular 8

I have an Angular application where I need to update some old AngularJS code to work with Angular table.html <table ngFor="let group of vm.groups" style="float: left"> <thead> <tr> <th><b>Sl. No</b ...

"Utilizing socket.io on a local server: A beginner's guide

When trying to implement socket.io in my localhost development environment, I encountered an issue: Client-side code: const socket = io.connect('https://localhost:4000/'); socket.emit('trim-movie/go', data); socket.on('trim-movie- ...

Dealing with issues in AngularJS when a directive modifies a value within a controller

Whenever I receive large images from an API in my primary controller, a specific directive informs me of each image being loaded: app.directive('imageonload', function() { return { controller: "mainController", restrict: &apo ...

I'm looking to upload an image to a Web API with Angular - Any suggestions on how

Here is the HTML code I have in my modal: <div id="name-group" class="form-group"> <label>Upload image *</label> <input type="file" name="img" class="form-control" ng-model="formData.img" > </div>` What do I need to ...

Can different classes be assigned as "dragenter" targets?

Is it possible to apply the Jquery "dragenter" event to multiple targets or classes simultaneously? I tried this approach, but it doesn't seem to be working: $('.list').on('dragenter','.class1, .class2', function(e) { ...

Utilize graphics in three.js for texture recycling

My challenge involves loading the same image in a large number (~ 1000) of two-dimensional shapes using three.js, each shape with different offsets. I came across this demo on the official website and modified it to create my own demo, featuring various s ...

Tips for retaining form inputs without the need for a submit event when the page is reloaded or refreshed

I have a form on a page with multiple text inputs In addition to the form, I have implemented Zend pagination to allow users to select results. However, when using Zend paginate, the user's form inputs are lost because it is not submitted. Since th ...

Error: The function of _data__WEBPACK_IMPORTED_MODULE_3___default.a.map is not executable

As a newcomer to React, I am exploring how arrays work with components using the map function. However, I recently encountered an error that has me stumped on how to resolve it. I have scoured numerous blogs for answers, but none seem to address my specif ...

jQuery for creating single-page websites with horizontal scrolling navigation

I'm attempting to implement a horizontal scrolling section on my webpage. Although this is a basic function that I have successfully executed multiple times in the past, I seem to be overlooking something rather obvious. There seems to be an issue w ...

Is it expected to conceal children who are 2 years old?

I came across the following HTML code snippet: <ul id="product_create-header" class="stepy-header"> <li id="product_create-head-0" class="stepy-active"> <div>Categoría</div><span>Categoría</span> </ ...

Ways to make multiple datepickers function with altFields

Within my application, there is a form dedicated to invoices, where each invoice can contain multiple items. Each item consists of a date input field (where users can enter the date in their preferred format) and a hidden field (for submission to the datab ...

Is it possible to trigger a JavaScript function without altering its existing value?

I am looking for a way to repeatedly call a JavaScript function without altering its previous values. The challenge I am facing is with an icon JSON object that contains 15 different icons. I am using a switch statement to execute the corresponding functio ...

"Mastering the Art of Design: Setting Hover Colors in angular-chart.js

Currently, I am utilizing the angular version of chartJS. My goal is to customize the hover color of the pie chart arcs. It seems that someone has already achieved this: here, but I am having difficulty understanding how they did it. Here is my attempt a ...

An effective method for adding a character to a number within an input field with the use of AngularJS

There is an input field in my code of type text that is connected to a variable within a certain scope. <input type=text= ng-model="vm.someProperty"> I am looking to have the input display the value with a percentage sign after it, without changing ...

How can I convert a standard RestApi into an HTTP POST request within an AngularJS application?

Is it possible to convert a regular RestApi into an HTTP post request using AngularJS? I am trying to send this URL as a $http post request within an AngularJS function. http://localhost:3001/ams/v2/folder?name=SampleCCCCCC This is the code I have: $ht ...

Is there a way to access specific methods within a JavaScript file?

Within my javascript assets folder, I have a file named jstester.js that looks like this: function hehe() { alert("wedsdsd"); } document.write("fdygsdfysdgf"); Then in the main index.html file in the public directory, I include the following code: & ...

What is the best way to switch between List and Grid view while automatically updating the image displayed?

**#Unique Component Implementation** import React from 'react'; import Typography from '@material-ui/core/Typography'; import Grid from '@material-ui/core/Grid'; import { makeStyles } from &apo ...

The error encountered is related to the MongooseServerSelectionError that occurs in

I have been working on setting up my first mongo-db application to connect to the server. However, I am encountering a specific error during this process: const express = require('express'); const mongoose = require('mongoose'); const ...