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;
}
});
2条答案
按热度按时间cnh2zyt31#
With some help from the answer of Amulya Kashyap this is my working solution:
The Filter a came up with:
jyztefdp2#
这是工作中的Fiddle of your code。
下面是您的代码:
HTML部分:
控制器代码