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

Encountering difficulty retrieving the value of a hidden input with jQuery's find method

When a user clicks on the delete icon, I want to retrieve the value of the hidden input inside the "delete" class. However, I am getting an undefined result. Can someone please guide me on where I might be going wrong? <table class="items-list"> ...

I am curious to know why my jQuery when().then() function is firing before the completion of the ajax request in the when clause

I have a situation where I need to set an async callback because a function is fetching content from a remote location. Here's what I am currently doing: $.when( priv[box.view.renderWith](content, box.view.gadget_id) ).then(function(late) { conso ...

Trouble with React Native ListView Item Visibility

I'm currently working on integrating the React Native <ListView /> component with the <List /> and <ListItem /> components from React Native Elements. However, I seem to be facing an issue where the <ListItem /> component is no ...

Substitute "Basic Authentication" with "Form Authentication"

Is there a way in W20 to switch from using "Basic Authentication" to "Form Authentication"? The current documentation mentions only the use of "basicAuth" and does not provide information on implementing form authentication. Our app is built with Angular ...

Choose the row based on the values in the columns

I am facing a situation where I have to choose a specific row in a datatable that displays rows in groups of 10, based on the value of its column. Previously, I successfully used the following code to select the first row. myGroupTable.row(':eq(0)&ap ...

What could be causing the input event not to be triggered consistently when I select or highlight text?

I have implemented a 4-digit pin field with a specific behavior: when a field is filled, the focus automatically shifts to the next field (the cursor moves to the next input field). If text in a field is deleted, the text in that field is also removed and ...

What is the best way to automatically change a variable in an AngularJS $scope?

My current project involves rendering a list of records to my HTML page, similar to this example taken from: https://www.w3schools.com/angular/ng_ng-repeat.asp <body ng-app="myApp" ng-controller="myCtrl"> <h1 ng-repeat="x in records">{{x}}&l ...

Is it possible to import data into a script?

When working with Angular, I am attempting to: $scope.data = "<script> alert('hi'); </script>"; Unfortunately, this approach does not seem to be effective. I also experimented with adding ng-bind-html but did not achieve any success ...

Seamless Axios operations even without internet connection in Vue.js

In my Nativescript Vue.js application, there is a functionality where the user clicks on login, Axios makes a call to an endpoint to fetch a token. However, I noticed that when the emulator phone is offline, the Axios call still goes through and the &apos ...

Change the class of each item in a list individually by hovering over them with the mouse using JavaScript

How to toggle classes in a list item one by one using the mouseover event in JavaScript? const items = document.querySelectorAll("ul li"); let currentItem; for (const item of items) { item.addEventListener("mouseover", e => { currentItem &am ...

What is the best way to implement auto-refreshing with reactQuery?

Hey there! I'm currently working with a GraphQL API and I'm trying to automatically refetch data at regular intervals (e.g. every 3 seconds). I've been using React Query and have tried some workarounds like using setInterval, but it's n ...

Displaying geoJSON data from a variable instead of a file is a feature implemented by

Let's say I have a geoJSON data stored in a variable or parsed as an object: // GeoJSON stored as an object var segment = segment; // GeoJSON saved as a JSON string var json = JSON.stringify(segment); Now, the challenge is to display this geoJSON o ...

In the absence of a value

In my code, I've implemented a functionality that saves the user's input into local storage and displays it in a specific ID. However, I want to make sure that if the input field is left empty, the user is prompted to enter their name. function ...

I possess a solitary div element that requires dynamic replication

I have a single container and an unspecified number of rows of data. I want to display this data on HTML cards that are generated dynamically based on the number of rows. For example, if there are 10 rows of data, I need to create 10 card elements with ea ...

Utilize the sortable script for dynamically loaded items via AJAX

For the drag and drop feature on my website, I am using jQuery sortable. I have a button that displays results with items on the screen. These items can be dragged and dropped into various sections. The issue I'm facing is that if I include the sort ...

Encountering an "XML Parsing Error: no element found Location" error in AngularJS $http when JSON results are expected on a GET request

Whenever I try to make an angularjs $http GET request, I am faced with an XML parsing issue. Here is the code snippet for the $http call: $http({ method: 'GET', url: "myapp/api/items" + itemId }); The error message I receive is as fol ...

Is it necessary to create event objects before setting event handlers in Leaflet JS?

I'm currently working on creating event handlers that respond to the success of the location() method in the leaflet JavaScript map class Here is my current implementation: function initializeMap() { var map = L.map('map').locate({setV ...

Requests exceeding 8 kilobytes

I am looking to measure the round trip time between a client and server. Users have the option to determine the size of the request/response message they want to send. On the client side, I am using the Ajax-Post method to transmit 100 messages every 100 m ...

A helpful tip on incorporating Snack Bar Material UI within an if statement

When my Camera Component encounters an error, I want to display a snackbar notification. I attempted to encapsulate the component within a function and pass the error message as props, calling it in the if statement. However, this approach did not work as ...

Steps to dynamically reveal a table row within another row when clicked:

I have a table and I want to show the contents of another row inside the current row when the plus icon is clicked. I'm not sure which HTML element to use for this. Please help me figure it out. Thank you in advance. <div class="pro ...