The ng-if directive is not increasing the $index value within the ng-repeat loop

$index does not increment within the ng-if block.

<div ng-repeat="household in census.households track by $index">
    <div ng-if="household.ask_for_tobacco">
       <input id="tobacco_{{$index}}" type="checkbox" ng-model="member.smoker">
       <label for="tobacco_{{$index}}"></label>
     </div>
</div>

In the above block, I am using ng-repeat and ng-if inside it. I am following the condition ng-if and setting the element ID with $index. The element ID is always being set to 0, instead of incrementing to 1, 2, 3...

If the ng-if is removed, it works without any issues. Have you faced a similar problem? If so, please share the solution.

Answer №1

The issue at hand is that ng-if generates its own scope, which can cause interference with the scope of the iteration created by ng-repeat.

Since using ng-show is not feasible, I recommend applying a filter to your ng-repeat as shown below:

<div ng-app="app" ng-controller="MyCtrl">
  <div ng-repeat="household in households | filter: { ask_for_tobacco: true }">
    <input id="tobacco_{{$index}}" type="checkbox">
    <label for="tobacco_{{$index}}">index {{ $index }} id = {{ household.id }}</label>
  </div>
</div>

Check out a live demo here: http://jsfiddle.net/m25afg12/9/

Answer №2

To access the parent index, simply use $parent.$index It may be a bit late for giving this answer though :D

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

Loop through XML nodes in Node.js

I am faced with a challenge of extracting specific data from a large XML document. The document can be accessed via the following link: https://pastebin.com/mNXWt7dz My goal is to parse the XML structure in order to retrieve values for each client-mac, cl ...

Is there a way to automatically select all checkboxes when I select contacts in my case using Ionic 2?

initializeSelection() { for (var i = 0; i < this.groupedContacts.length; i++) { for(var j = 0; j < this.groupedContacts[i].length; j++) this.groupedContacts[i][j].selected = this.selectedAll; } } evaluateSelectionStatus() { ...

Exploring the capabilities of useRef in executing a function on a dynamically created element within a React/Remix/Prisma environment

I've been trying to implement multiple useRef and useEffect instructions, but I'm facing difficulties in getting them to work together in this scenario. The code structure involves: Remix/React, fetching data from a database, displaying the data ...

Creating an interactive checkbox input field utilizing a JSON-driven database

Can someone assist me with creating dynamic checkboxes? I have JSON data structured as follows: [ { "categoryName": "Category 1", "items": [ { "value": "value1", "label": "label1" }, { "value": "value2" ...

The onChange event was not able to be activated within the material-ui radioGroup component

Utilizing the RadioButton component to showcase different options of a question within a custom-built component: import FormControl from "@material-ui/core/FormControl"; import FormControlLabel from "@material-ui/core/FormControlLabel"; import Grid from " ...

Automatically select the option from the drop-down menu

Click on me button will add a pink block each time it is clicked. Each pink block contains the same menu options to edit, delete, and run. When a new pink block is added by clicking the button, it should automatically open the edit menu of that specific ...

What is the best way to assign a unique background color to each individual card in my card list?

I have a list of cards and I want to assign a unique color to each card in the list. I attempted to do this on my own, but it ended up being more complex than I anticipated. Here is the list of cards: return ( <Container> <Row className=&qu ...

When attempting to launch the Ember Cordova application on the mobile device, an error was encountered stating: "Unable to access property 'notification' as it is

After installing cordova-plugin-local-notifications and trying it again, I encountered an error on the following line of code: cordova.plugins.notification.local.on("click", function (notification) { The error message reads: Error while processing rou ...

Ajax User is currently composing a message in the chat

I'm currently working on creating a user typing system similar to Facebook. However, I have a question regarding keypress events. My code is functioning properly, but I want to make changes to other events like keyup, paste, etc. Below are the JavaS ...

Purge ajax result upon completion of server operation

I'm currently working with an AJAX code that takes input from a radio form. The server code, upon satisfying a specific if() condition, redirects to another page on my site, such as dashboard.php! However, upon redirection, I am still seeing the rad ...

Modifying the appearance of a CSS element with jQuery: Step-by-step guide

The code I have is as follows: $('.signup-form-wrapper').css("style", "display: block"); $('.login-form-wrapper').css("style", "display: none"); I'm not sure why it's not working. The current appearance of ...

I am in need of a efficient loader for a slow-loading page on my website. Any recommendations on where to find one that works

I'm experiencing slow loading times on my website and I'm looking to implement a loader that will hide the page until all elements are fully loaded. I've tested several loaders, but they all seem to briefly display the page before the loader ...

Not entirely certain about how to execute this concept using AJAX

While pondering the development of an instant messaging application, I aimed to decrease the frequency of AJAX requests (currently one every .2s). This led me to devise a unique approach: Initiate an AJAX request from the user side to the server. Wait ...

IntelliJ is not able to detect certain JS libraries

Currently, I am working on an NDV3 AngularJS graph demo using IntelliJ. To start off, I have created a standard HTML file named index.html and included all the necessary AngularJS and NDV3 libraries in a folder called bower_components located within the r ...

Toggle the tooltip to reveal a hidden div by clicking, and then click again to close it

I've been working on implementing a toggle div with a tooltip. The issue I'm facing is that the tooltip initially displays "Click to open", but after opening the toggle, it should change to "Click to close". Can someone assist me in achieving thi ...

Implement a method to duplicate and eliminate elements in a fast and effective manner

I am in need of assistance. The challenge I am facing is related to cloning hidden text and ensuring that the clone can be removed independently. Currently, when the hidden text is cloned, the remove function does not work properly on the clone. Instead, ...

Adding up the array of values that have been increased

Looking to create a function that calculates the sum of an array based on two input values. The first input value is added incrementally until it reaches the second input value. For example, if the lowerLimit is '2' and the upperLimit is '5& ...

Using radio buttons and a price slider, a unique jQuery filter for products can be

I have successfully implemented basic functionality to filter products based on price (slider) and radio boxes. However, the current filter system uses OR logic, but I need it to use AND instead. For instance, I want to find a product that is from Brand1, ...

Is it possible to utilize Angular's dependency injection in place of RequireJS?

As a newcomer to Angular, I am wondering how I can organize my code into separate files without using requirejs or any other framework. After watching a brief introductory video, it seemed possible. Currently, my app looks like this and functions well: v ...

Is there a way to configure multiple entry points in a package.json file?

I am looking to execute various commands using npm: "scripts": { "v1": "node v1.js", "v2": "node v2.js" } I would like to use npm start v1 or npm start v2 for this purpose, however, these commands do not seem to trigger the intended Node command. ...