What is the best way to attach a function as an Angular filter predicate?

Struggling with the binding of a function to 'this' in my typescript and angular project. It's important to note that the controller and $scope are distinct entities in this scenario.

Tried using angular.bind(this, this.filterViewedStagingItems); without success. Resorting to an inlined function for now, which feels like a less than ideal workaround.

Html:

<div class="table-entry row table-entry stagingItem" ng-repeat="stagingItem in stagingItems|filter:vm.filterViewedStagingItems">

Code:

export interface IFooController extends ng.IScope {
    vm: Controller;
    isNewForSupplier:boolean;
    isNewForRestaurant:boolean;
    isPackSizeDescChanged:boolean;
    isDescChanged:boolean;
}

export class Controller{
    public static $inject = [
        '$scope',
        '$rootScope',
        '$routeParams'
    ];

    constructor(private $scope:IFooController,
                $rootScope: ng.IScope,
                private $routeParams:any)
    {
        $scope.vm = this;
        angular.bind(this, this.filterViewedStagingItems);//Doesn't work
    }

    private filterViewedStagingItems(stagingItem: StagingItem): boolean
    {
        if (this.$scope.isNewForRestaurant && stagingItem.isNewForRestaurantOnly())
        {
            return true;
        }

        if (this.$scope.isNewForSupplier && stagingItem.isNewItemOnly())
        {
            return true;
        }

        if (this.$scope.isDescChanged && stagingItem.isDescriptionChangedOnly())
        {
            return true;
        }

        if (this.$scope.isPackSizeDescChanged && stagingItem.isPackSizeChangedOnly())
        {
            return true;
        }

        if (this.$scope.isPackSizeDescChanged && this.$scope.isDescChanged && stagingItem.isDescriptionOrPackSizeDescChangedOnly())
        {
            return true;
        }

        if (this.areAnyFiltersEnabled()) {
            return false;
        }
        return true;
    }

    private areAnyFiltersEnabled():boolean
    {
        return this.$scope.isNewForRestaurant || this.$scope.isNewForSupplier || this.$scope.isDescChanged || this.$scope.isPackSizeDescChanged
    }
}

Answer â„–1

To simplify, just implement a lambda function:

const filterViewedStagingItems = (stagingItem: StagingItem): boolean => {
}

If you're unsure about how a lambda works, check out this video explanation: https://www.youtube.com/watch?v=tvocUcbCupA&hd=1

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

Tips for simulating difficult private attributes within a class during unit testing in TypeScript

Is there a way to mock the value of a hard private property in a unit test? For example, how can I expect something like expect(event.getEventHis()).toBeEqual(['a', 'b']) export class EventController { #event: []; constructor() { ...

Ways to show error at the top of the page rather than next to the input HTML element

Is there a way to show errors at the top of the page instead of next to the input HTML element? Please provide suggestions. <ng-form name="frmdisbursementScheduleMaintenance"> <div style="padding-bottom: 8px;"> &l ...

How come Protractor always returns a value of 0 when using WebElement.getAttribute('value') on a list-item-element with a value attribute?

I need help with a directive that includes the following code snippet <ol id="singleSelection" class="nya-bs-select" ng-model="model"> <li class="nya-bs-option" value="a"> <a>Alpha</a> </li> <li class="nya-bs-opt ...

Collect form input data containing a binary image and showcase it in a view using webapi

I am currently working on a webapi project using MVC architecture. The project involves converting a jpg image to binary data, sending it to a SQL server, and then retrieving the form data along with the image back as a jpg for display on the view. Althoug ...

Flag form field as invalid in AngularJS

Struggling to implement server-side form validation in an AngularJS app? Finding it tricky to invalidate a form field and show an error message? Here's the setup of my app: I have a model 'client' with a controller Accounts.controller(&ap ...

At runtime, the array inexplicably becomes null

Having recently ventured into the world of Ionic framework development, I have encountered a puzzling issue. At runtime, an array inexplicably gets nulled and I am struggling to pinpoint the root cause. export interface Days { name:string; } @Compon ...

Is there a way to set the page language in servlet or jsp to ensure angularjs recognizes it?

Ensuring that AngularJS form validation messages always display in French on a specific web page is crucial. While they show correctly in French-language browsers, the issue arises when viewed in an English-language browser. I am refraining from detecting ...

Conceal dynamically generated div elements created with ngIf

I am currently working on initializing this div using ngOnInit in Angular ngOnInit(): void { let optTemp = ''; for (let data of arrOption) { optTemp = optTemp + '<option>' + data.trim() + '</option> ...

Can you explain the process of utilizing Angular databinding to display nested information?

I'm facing a challenge with databinding when working with nested arrays in multiple timeslots and windows. Despite understanding the basics, I can't seem to make it work no matter how I try different approaches. It's really frustrating not k ...

Is there a way to use TestCafé to simultaneously log into multiple services?

I've been experimenting with testcafé in an effort to simultaneously log into multiple services using the role mechanism. My goal is to have my tests logged into multiple services concurrently without switching between roles. While a guide on this t ...

`What can be done if ng-if is not responding?`

I'm facing an issue where I want to display a link <a href> only when a certain condition is met, but the link doesn't show up as expected. I have already attempted to experiment with changing the position of the code (inside or outside of ...

Expanding the Window Object in Typescript with Next.js Version 13

Within my Next.js 13 project, I am looking to enhance the Window object by adding a property called gtag I created an index.d.ts file in the root folder with the following content: index.d.ts declare module '*.svg' { const content: any; exp ...

Prevent ng-repeat from rendering the contents of my div

Currently, I am in the process of modifying a website for which I need AngularJS. I am trying to use the ng-repeat command to display some documentation on the site. However, whenever I add the ng-repeat within a div, it seems to "destroy" the layout and I ...

Executing functions in TypeScript

I am facing an issue while trying to call a function through the click event in my template. The error message I receive is "get is not a function". Can someone help me identify where the problem lies? This is my template: <button class="btn btn-prima ...

Issue: Module not found error encountered while applying decorators

I am attempting to incorporate decorators into my TypeScript class. Here is what I have done: import { Model, ModelCtor } from 'sequelize/types'; function decorator(constructor) { // } @decorator class Service implements IService { // ...

Tips on deleting the last character in an input field with AngularJS

Can you help me with the title of this question? I'm currently working on developing a straightforward calculator using AngularJS. It's operational at the moment, but I'm looking to incorporate additional buttons like a "delete" key and a de ...

Challenges with validating forms in AngularJS

I've been attempting to implement a basic form validation using AngularJS, but for some reason it's not working as expected. I can't seem to figure out what I'm missing. HTML Form Code <form role="form" class="ng-pristine ng-va ...

React TypeScript: The properties of 'X' are not compatible. 'Y' cannot be assigned to 'Z' type

I am currently working on a React-TypeScript application, specifically creating a component for inputting credit card numbers. My goal is to have the FontAwesome icon inside the input update to reflect the brand image as the user enters their credit card n ...

ExpressJS and Angular: Issue with Loading the Index Page

I'm encountering a challenge while working on an application that incorporates expressjs and angular. The setup involves loading angular, bootstrap, and other libraries from the index page. During testing, I added a button <a ui-sref=about>about ...

What is the best approach for creating routes with parameters of varying lengths?

Due to the structure of the website's url, the parameters will vary, making it impossible to set a fixed number of parameters. How can we modify the app-routing.module.ts file to accommodate this? url => /products/cat1/cat2/cat3/cat4 ... const rou ...