Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular JS filter equals

Please see the details. Why is the output wrong?

HTML:

<div ng-app>
    <div ng-controller="TodoCtrl">
         <h1>List</h1>

        <div ng-repeat="t in todos | filter:{ id: '-1'}">{{t.text}}</div>
    </div>
</div>

Angular code:

function TodoCtrl($scope) {
    $scope.todos = [{
        text: 'learn angular',
        done: true,
        id: -1
    },{
        text: 'learn angular 2',
        done: true,
        id: -11
    }, {
        text: 'build an angular app',
        done: false,
        id: 1
    }];
}

Output:

learn angular
learn angular 2

Please see:

filter:{ id: '-1'}

Why does the output include:

learn angular 2

I want to search the id -1, but learn angular 2 is -11

like image 460
Footabll.My.Life Avatar asked Nov 14 '14 16:11

Footabll.My.Life


People also ask

How does filter work in angular?

The “filter” Filter in AngularJS is used to filter the array and object elements and return the filtered items. In other words, this filter selects a subset (a smaller array containing elements that meet the filter criteria) of an array from the original array.

What is $filter in AngularJS?

Filters are used for formatting data displayed to the user. They can be used in view templates, controllers or services. AngularJS comes with a collection of built-in filters, but it is easy to define your own as well.

How do I filter data in ng repeat?

The ng-repeat values can be filtered according to the ng-model in AngularJS by using the value of the input field as an expression in a filter. We can set the ng-model directive on an input field to filter ng-repeat values.


1 Answers

Remove the quotes from '-1' and send true to the filter to do a strict comparison.

ng-repeat="t in todos | filter:{ id: -1}:true"

like image 65
Ankur Agarwal Avatar answered Sep 29 '22 12:09

Ankur Agarwal