Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ng-options with track by and filters

Hey so I need to populate my select box with a list of Regions from Json object. There are duplicates in here that I would like to remove and I have set the alphabetical order.

I also need to use track by job.WS_REGION so that the value is set to the region just now I think it is tracking index so assigning a number to the value.

Can track by be used in conjunction with filters?

They work perfect independent of each other either by assigning value or applying filters.

<select ng-model="filterzz" ng-options="job.WS_REGION for job in jobsfull track by job.WS_REGION | orderBy: 'WS_REGION' | unique:'WS_REGION'">

If this is a simple mistake go easy on me its a Friday afternoon :)

like image 563
craigie2204 Avatar asked Sep 12 '14 14:09

craigie2204


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.

What is ng-options?

The ng-options Directive in AngularJS is used to build and bind HTML elements with options to a model property. It is used to specify <options> in a <select> list. It is designed specifically to populate the items on a dropdown list. This directive implements an array, in order to fill the dropdown list.

How do I set default selected value in ng-options?

In my opinion the correct way to set a default value is to simply pre-fill your ng-model property with the value selected from your ng-options , angular does the rest. Essentially when you define the $scope property your select will bind to assign it the default value from your data array.

What does track by do in angular?

"track by" tells the angular js that how angular js will track the association between DOM and the model (i.e. collection). Internally angular js uses "track by $id(obj)" for this purpose. You can use track by $index if you do not have a unique identifier.


1 Answers

Try parentheses:

ng-options="job.WS_REGION for job in (jobsfull | orderBy: 'WS_REGION' | unique:'WS_REGION') track by job.WS_REGION"
like image 53
Nikos Paraskevopoulos Avatar answered Oct 30 '22 17:10

Nikos Paraskevopoulos