Ionic Angular 自定义滤镜:将值与数组中的值匹配并获取相应的名称

zzlelutf  于 2022-12-08  发布在  Ionic
关注(0)|答案(2)|浏览(167)

Blockquote
I want to convert a number to a animal with a Angular filter.
I have the following ng-repeat:

<div ng-repeat="speed in speeds track by $index">
   You're faster than a <span ng-bind="speed.value | speedToAnimal"></span>
</div>

speed.value wil give you a value like 42.9
Ik want to match this speed value with a speed value in a array where speeds are linked to animals.
For example this is the array I have:

var animals = [
    {
      "speed": 33.7,
      "animal": 'pig',
    },
    {
      "speed": 40.2,
      "animal": 'Dog',
    },
    {
      "speed": 45.4,
      "animal": 'Horse',
    }
    ...
];

The speed value 42.9 is higher than the speed value of the dog in the array but lower than the speed value of the horse so I want the filter to turn the value 42.9 in to "Dog" so the end result wil be: You're faster than a Dog, instead of You're faster than a 42.9
I know how to call a filter but I don't know how build one.
Call filter: ng-bind="speed.value | speedToAnimal
filter:

app.filter('speedToAnimal', function() {
  return function(input, optional1, optional2) {
    // Do filter work here
    return output;
  }
});

jsfiddle

cnh2zyt3

cnh2zyt31#

With some help from the answer of Amulya Kashyap this is my working solution:

<div ng-repeat="speed in speeds track by $index">
   You're faster than a <span ng-bind="speed.value | speedToAnimal"></span>
</div>

The Filter a came up with:

app.filter('speedToAnimal', function () {
    return function (value) {

        var animals = [
            {"speed":12,"animal":"Field Mouse"},
            {"speed":13,"animal":"House Mouse"},
            {"speed":16,"animal":"Camel"},
            {"speed":18,"animal":"Pig"},
            {"speed":20,"animal":"Gray Squirrel"},
            {"speed":22,"animal":"Sea Lion"},
            {"speed":24,"animal":"Sheep"},
        ];

        if(!animals) return;

        var curSpeed = value;
        var len = animals.length - 1;
        while(len >= 0){
            if(curSpeed >= animals[len].speed){
                return animals[len].animal;
            }
            len--;
        }
    };
});
jyztefdp

jyztefdp2#

这是工作中的Fiddle of your code
下面是您的代码:

HTML部分:

<div ng-controller="MyCtrl">
 <legend> Enter Speed</legend> 
<input type="text" ng-model="speed.value" required />
<button type="button" ng-click="calc()">Get Animal</button>

<span>{{filteredAnimals}}</span>
</div>

控制器代码

var myApp = angular.module('myApp',[]);

myApp.controller('MyCtrl', function($scope) {
    $scope.animals = [
    {
      "speed": 33.7,
      "animal": 'pig',
    },
    {
      "speed": 40.2,
      "animal": 'Dog',
    },
    {
      "speed": 45.4,
      "animal": 'Horse',
    }];
    
    $scope.calc = function(){
        $scope.curSpeed = $scope.speed.value;
        var len = $scope.animals.length - 1;
        var filteredAnimals;
        while(len >= 0){
            if($scope.curSpeed >= $scope.animals[len].speed){
                filteredAnimals = $scope.filteredAnimals = $scope.animals[len].animal;
                break;
            }
            len--;
        }
    }
});

相关问题