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
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.
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.
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.
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With