Access index of the parent ng-repeat from child ng-repeat

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for Access index of the parent ng-repeat from child ng-repeat

👋 Hey there, tech lovers! Welcome to our blog, where we make the complicated seem easy! Today, we have a common issue that many developers stumble upon when working with nested ng-repeats. Let's dive right in and tackle it together! 🤓

The problem at hand is how to access the index of the parent ng-repeat from within the child ng-repeat. In the given context, the aim is to use the index of the parent list, foos, as an argument in a function call within the child list, foos.bars. Pretty tricky, right? 😅

The original suggestion was to use $parent.$index, but it seems that $index is not a property of $parent. So let's find a different approach to make it work!

To access the index of the parent ng-repeat, you can make use of a feature called $parent.$index in combination with an alias. Let's see how it works:

<div ng-repeat="f in foos">
  <div>
    <div ng-repeat="b in foos.bars as parentIndex"> <!-- an alias, let's call it parentIndex here! -->
      <a ng-click="addSomething(parentIndex)">() Add Something</a>
    </div>
  </div>
</div>

In the example above, we introduced the alias parentIndex in the child ng-repeat declaration. By doing so, we create a new scope for the child ng-repeat, enabling us to access the parent index of foos.

Now, when we call the addSomething function with parentIndex as an argument, we are effectively passing the parent index to that function. 😎

And there you have it! A simple solution to a common problem! 🎉

But hey, it doesn't stop here! We'd love to hear from you! Join the conversation by leaving a comment and sharing your experiences with nested ng-repeats or any other Angular challenges you've encountered!

Also, don't forget to share this post with your fellow developers who might be struggling with the same issue. Together, we can make coding a breeze! 💪🔥

Happy coding! 👩‍💻👨‍💻

Take Your Tech Career to the Next Level

Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my