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

Resolving the "n timer(s) still in the queue" Error In Angular Unit Tests

How to Get Norton Security Suite Firewall to Allow Remote Desktop Connections in Windows

How to Determine if a Column Exists in a DataReader

Fixing the "Please add a @Pipe/@Directive/@Component annotation" Error In An Angular App After Upgrading to webpack 4

Silent Renew and the "login_required" Error When Using oidc-client