Angular Filter Negate Predicate Issue And Workaround

I recently ran into a funny quirk when attempting to specify a predicate negating condition on a filter in Angular.js.

According to the Angular docs:
The predicate can be negated by prefixing the string with!. For example {name: "!M"} predicate will return an array of items which have propertyname not containing "M".

First, here's what I was attempting to do:

<div ng-repeat="section in newsletter.sections | filter: { sectionType: !1 } | orderBy:['region','order']">

In the example above, I'm attempting to filter out any sections of my newsletter object which have a sectionType other than 1. However, this wouldn't do the trick.

The workaround was to append the exclamation mark as a string, as shown below:

<div ng-repeat="section in newsletter.sections | filter: {sectionType: '!' + 1} | orderBy:['region','order']">

I guess when they said "prefixing the string", they meant that literally, However, in my case, the value I was filtering by wasn't a string, but rather a number. But appending the exclamation mark as a string did the trick.

Comments

Popular posts from this blog

How To Mock Out Child Components In Unit Tests of Angular 2 Code

A Generic Method Using HttpClient to Make a Synchronous Get Request

The Cause and Solution for the "System.Runtime.Serialization.InvalidDataContractException: Type 'System.Threading.Tasks.Task`1[YourTypeHere]' cannot be serialized." Exception