Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJs conditional display for ng-options

I've been searching for a few hours now and I can't seem to find the answer for my problem anywhere. I remember that I was able to implement it before but for some reason I lost the code and I can't seem to remember what I did to make it work.

I'm trying display data from an array in a select box using ng-options. That one I have no problems with. What I need to implement is that only certain array values will be displayed/included in the select box.

My data is as follows:

$scope.chartList = [ { "id" : 1, "name" : "chart 1", "order" : 1, "active" : false },
                     { "id" : 2, "name" : "chart 2", "order" : 2, "active" : true },
                     { "id" : 3, "name" : "chart 3", "order" : 3, "active" : true },
                     { "id" : 4, "name" : "chart 4", "order" : 4, "active" : true }, 
                     { "id" : 5, "name" : "chart 5", "order" : 5, "active" : true } ];

And my HTML looks like:

<select ng-model="toAddChart" ng-options="chart.id as chart.name for chart in chartList | filter:chart.active=='false'">
  <option value=""></option>
</select>

So what I want to happen is that if the value of the attribute "active" is false then it's the only time the array item will be displayed/included in the select list. I've tried different permutations of the filter attribute but none seem to work.

I know I can easily use ng-repeat in the tag and use ng-show but I remember reading somewhere (again, I can't find where) that it's not the proper way to implement it and that using ng-options is the correct way.

I'm really really sure I was able to do it before without creating a custom javascript filter, but for the life of me I can't remember how I did it. I'm hoping someone can

Hope someone can help me with this because I'm out of ideas.

Update:

Great scott, I think I've got it.

Instead of:

filter:chart.active=='false'

It should be:

filter:chart.active='false'

It's just the number of equal signs used. facepalm

Thanks for the responses, everyone.

like image 619
Stellaire Avatar asked Oct 02 '13 06:10

Stellaire


2 Answers

Great scott, I think I've got it.

Instead of:

filter:chart.active=='false'

It should be:

filter:chart.active='false'

It's just the number of equal signs used. facepalm

Thanks for the responses, everyone.

like image 109
Stellaire Avatar answered Oct 02 '22 03:10

Stellaire


Hey Instead its better use

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

like image 20
Prafull Avatar answered Oct 02 '22 04:10

Prafull