Using ng-Options as I do filter that interprets exactly the reported value

2

I'll go through an example just for testing:

In this example when I put filter:chart.id='1' , in addition it returns me the id 1 it returns me the id 10, how would I make it return only the id = 1?

function TodoCtrl($scope) {
    
  $scope.chartList = [ 
    { "id" : 1, "name" : "chart 1", "order" : 1, "active" : false },
    { "id" : 2, "name" : "chart 2", "order" : 2, "active" : false },
    { "id" : 3, "name" : "chart 3", "order" : 3, "active" : true },
    { "id" : 4, "name" : "chart 4", "order" : 4, "active" : true }, 
    { "id" : 10, "name" : "chart 10", "order" : 5, "active" : true }
  ];
}
.done-true {
  text-decoration: line-through;
  color: grey;
}
<div ng-app>
  <div ng-controller="TodoCtrl">
    <select ng-model="toAddChart" ng-options="chart.id as chart.name for chart in chartList | filter:chart.id='1'">
      <option value=""></option>
    </select>
  </div>
</div>
    
asked by anonymous 31.03.2015 / 20:28

1 answer

3

Depending on the version of AngularJS you are using you can force it to make an exact comparison by passing true as parameter to the filter

ng-options="chart.id as chart.name for chart in chartList | filter:chart.id=1:true"

But you should take into account that the comparison of this form is accurate, so in your example there were single quotation marks in filter:chart.id='1' , notice that I removed them in my example, with them it would not work because it would compare a text with a number and the result would be false.

In old versions of AngularJS you can create a function to make this filter

$scope.meuFiltro = function(valor){
    return function(obj){
        return obj.id == valor;
    }
};

Then you would use it by passing the value you want to filter

ng-options="chart.id as chart.name for chart in chartList | filter:meuFiltro(1)"
    
31.03.2015 / 22:40