Conditionally displaying ng-options in AngularJSI'm looking for a

After spending hours searching, I'm unable to find a solution to my problem. I was able to implement it before but lost the code and can't remember how I did it.

I need to display only certain array values in a select box using ng-options. The data I have is:

$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 } ];

The HTML looks like this:

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

I want to only show items in the select list where the attribute "active" is false. I've tried various options of the filter attribute without success.

I recall that using ng-repeat in the <option> tag with ng-show is not the correct way to do this, according to what I read somewhere. Using ng-options is supposed to be the proper method.

I'm sure I was able to achieve this without creating a custom JavaScript filter in the past, but I can't remember how. Any assistance would be greatly appreciated.

Update:

I think I figured it out.

Instead of:

filter:chart.active=='false'

It should be:

filter:chart.active='false'

The number of equal signs was the issue. *facepalm*

Thank you to everyone who responded.

Answer №1

Well butter my biscuit, I reckon I've cracked the code.

Rather than:

filter:chart.active=='false'

The correct syntax is:

filter:chart.active='false'

I can't believe it was just a matter of the number of equal signs. *facepalm*

A big thank you to all who provided feedback!

Answer №2

Hey, why not utilize,

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Determine the presence of either one or both values in a JavaScript object

I was looking at an Object like this: {"user": { "name": "Harry Peter", "phoneNumber": "12345", "products": [ { "type": "card", "accountId": "5299367", }, { "type": "Loan", ...

The controller function is not triggered if the user does not have administrator privileges

I have a code snippet in which my controller function is not being triggered for users who do not have the role of "Admin". Can someone help me identify where I went wrong? Just to clarify, the controller function works fine for users with the role of "Adm ...

When attempting to navigate to a controller using Express.Router and Passport, encountering an undefined error with req.url.indexOf('?')

The statement "var search = 1 + req.url.indexOf('?');" throws an error indicating that it is undefined. I am currently working on creating a login/registration page using passportjs on my angular frontend. When attempting to make a post request t ...

HtmlUnitDriver fails to execute javascript while loading a page from a URL

My issue revolves around testing my website page, where elements displayed with javascript are missing when viewed through the HtmlUnitDriver. I am currently using selenium-java version 3.141.59 and htmlunit-driver version 2.33.3. Below is a snippet of my ...

angular.js watch() method is not functioning properly during a JSON call

I am trying to trigger a method whenever the value of my $http.selectedSong (model value) changes, but for some reason it is not working. Any ideas on why this could be happening?: app.controller('songController', ['$http', function($h ...

Auto-scroll feature malfunctioning

My auto scroll function using jQuery isn't working, here is my CSS: #convo_mes{ text-align:left; width:98%; height:80%; background:#fff; border:1px solid #000; overflow-x:auto; } And in my JavaScript: $(".mes").click(functio ...

Convert a JSON array into a JavaScript array?

Similar Question: How can I extract property values from a JavaScript object into an array? I am receiving a JSON array from a basic server and need to parse it into JavaScript for use in my web application. What is the best way to convert a JSONP ar ...

Arranging Data in AngularJS based on Selected Filter Criteria

I have a filter function that currently only returns the name values in my table. I would like to add options for both ascending and descending sorting to this filter. Progress So Far: I am able to retrieve values from all inputs, including the name and ...

What tips can you provide for shrinking the size of an AngularJS website with the help of Google Closure Compiler?

Is there a way to decrease the size of an Angularjs site using Google Closure Compiler? I have a website built on Angularjs 1.8.x and I am interested in compiling it with Closure. Are there any examples or demonstrations available to help me achieve this ...

Tips for concealing XHR Requests within a react-based single page application

Is there a way to hide the endpoint visible in Chrome's devtools under the network tab when data is fetched in React? Can server-side rendering solve this issue? ...

Merging two arrays together in JavaScript

Within my possession are two arrays: var g= [ { id: 36, name: 'AAA', goal: 'yes' }, { id: 40, name: 'BBB', goal: 'yes' }, { id: 39, name: 'JJJ', goal: 'yes' }, { ...

Days and Their Corresponding Names Listed in Table

Currently, I am working on creating a "timesheet" feature that requires the user to input 2 dates. The goal is to calculate each month and year based on the input dates and then display them in a dropdown menu combobox. I would like to dynamically update ...

Tips for nesting maps in React without causing redundant renders

I have a list of all the items in the array let products = [ { name: "iPhone 12", storage: "64GB" }, { name: "iPhone 12 Pro", storage: "256GB" }, { name: "iPhone 12 Pro Max", storage: "512GB" ...

Update your Selenium WebDriver Manager using npm

When attempting to update the selenium webdriver using the "webdriver-manager", an error occurred: Error: Got error Error: read ECONNRESET from https://selenium-release.storage.googleapis.com/2.48/selenium-server-standalone-2.48.2.jar Error: Got error Err ...

Tips for integrating both client-side and server-side validation techniques in your web application

After conducting some research, I have come across information indicating that it is feasible to implement both client-side and server-side validation simultaneously. However, my focus is not solely on learning JavaScript; I am also interested in utilizi ...

Utilizing ActivatedRoute in conjunction with another Service

I am facing an issue while trying to utilize the ActivatedRoute service in a different service. The problem lies in the fact that when I use the ActivatedRoute service in my service, it is observing the main App component and not picking up any route param ...

Exploring Nodejs and delving into fundamental queries

As I delved into the world of Nodejs, I quickly realized that my knowledge was limited to just being a user. Transitioning to creating my own server posed challenges that were not easily resolved by online tutorials. This led me here seeking guidance to tr ...

Is there a way to distribute my World instance among several step definition files in CucumberJS?

Currently, I am working on implementing a CucumberJS scenario that involves using multiple steps spread out across two separate step definition files. In this setup, the first step establishes certain variables in the World object which need to be accessed ...

Trouble with implementing an onclick event listener in a foreach loop

While generating and adding HTML in a for loop, I noticed that adding onclick events within the same loop is not functioning correctly: items.forEach(item => { itemHtml = `<div class="${item.id}">${item.id}</div>`; $(".it ...

Podcast Audio Feed Reader App

As a newcomer to the world of podcasting, I am seeking guidance on how to incorporate my Pinecast-hosted Podcast's RSS feed into my website. Is there an Angular/Bootstrap RSS Feed Reader available that can seamlessly display each episode with its tit ...