Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use filter on ng-options to change the value displayed

I have an array of prices (0, 0.99, 1.99... etc) that I want to display in <select>.

I want to use Angular's ng-options like this

<select ng-model="create_price" ng-options="obj for obj in prices"/> 

As it is displayed above it will generate a selection of 0, 0.99, 1.99...

But I want to use a filter in the code such that every time the word 'prices' is presented (or something like that), the code will run a function to change the float numbers to strings and present (free, 0.99$, 1.99$... etc).

I there a way to do that?

Thanks

like image 273
TidharPeer Avatar asked May 20 '13 07:05

TidharPeer


People also ask

How do I filter with Ng-options?

In AngularJS when you are using ng-options, you can filter out the options by calling a custom function you have in the controller. Let's say you have following set of employees and when you display all these employees inside HTML select using ng-options, you can see all the employees in a dropdown.

Can we use filter in NG model?

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. The below illustrations describes the approach for the implementation.

How do ng-options work?

Definition and Usage. The ng-options directive fills a <select> element with <options>. The ng-options directive uses an array to fill the dropdown list. In many cases it would be easier to use the ng-repeat directive, but you have more flexibility when using the ng-options directive.

What is correct way to apply multiple filters in angular?

Using filters in view templates Filters can be applied to the result of another filter. This is called "chaining" and uses the following syntax: {{ expression | filter1 | filter2 | ... }} E.g. the markup {{ 1234 | number:2 }} formats the number 1234 with 2 decimal points using the number filter.


2 Answers

There's a better way:

app.filter('price', function() {   return function(num) {     return num === 0 ? 'free' : num + '$';   }; }); 

Then use it like this:

<select ng-model="create_price" ng-options="obj as (obj | price) for obj in prices"> </select> 

This way, the filter is useful for single values, rather than operating only on arrays. If you have objects and corresponding formatting filters, this is quite useful.

Filters can also be used directly in code, if you need them:

var formattedPrice = $filter('price')(num); 
like image 107
shawkinaw Avatar answered Oct 16 '22 07:10

shawkinaw


You want to create the custom filter such as:

app.filter('price', function() {   return function(arr) {     return arr.map(function(num){       return num === 0 ? 'free' : num + '$';     });   }; }); 

use it like:

<select ng-model="create_price" ng-options="obj for obj in prices | price">   {{ obj }} </select> 
like image 29
Tosh Avatar answered Oct 16 '22 07:10

Tosh