Calling a function when ng-repeat has finished

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for Calling a function when ng-repeat has finished

🎉 Calling a Function when ng-repeat has Finished Rendering! 🎉

So you want to know how to call a function when ng-repeat has finished rendering? Looks like you're in the right place! 🙌

The Challenge

Here's the thing, my friend. You're not alone on this one. Many developers have faced this exact issue. They want to perform some action or call a function once all the elements in the ng-repeat have been rendered on the page. But how the heck do we do that? 🤔

The Solution

Fear not, for I have the answers you seek! 💪 Here's a simple solution that will make your life a whole lot easier:

  1. First, let's create a custom Angular directive, onFinishRender, that will handle our magic:

    var module = angular.module('testApp', []) .directive('onFinishRender', function () { return { restrict: 'A', link: function (scope, element, attr) { if (scope.$last === true) { element.ready(function () { // CALL YOUR FUNCTION HERE! console.log("calling:" + attr.onFinishRender); }); } } } });
  2. Now, in your HTML, add the on-finish-render attribute to the element inside the ng-repeat:

    <div ng-app="testApp" ng-controller="myC"> <!-- Add the on-finish-render attribute and specify the function to call --> <p ng-repeat="t in ta" on-finish-render="test()">{{t}}</p> </div>
  3. Finally, define your function in the controller. In this example, let's call it test():

    function myC($scope) { $scope.ta = [1, 2, 3, 4, 5, 6]; // Define your function here function test() { console.log("test executed"); } }

💡 Pro Tip!

If you want to take things up a notch and make your code cleaner and more reusable, you can pass arguments to your onFinishRender directive. For example:

<div ng-repeat="t in ta" on-finish-render="test(param1, param2)">{{t}}</div>

And in your directive:

// ...
link: function (scope, element, attr) {
    if (scope.$last === true) {
        element.ready(function () {
            // Get the function name and arguments from the directive attribute
            var fn = attr.onFinishRender.split('(')[0];
            var args = attr.onFinishRender.split('(')[1].split(')')[0].split(',');
            
            // CALL YOUR FUNCTION HERE!
            scope[fn].apply(null, args);
        });
    }
}
// ...

🚀 The Final Result

And there you go! 🎉 Now, your function will be called once all the elements in the ng-repeat have finished rendering. Test it out, see it in action, and impress your friends with your newfound knowledge! 🔥

Do you have any other questions or suggestions?

Feel free to leave a comment, share your thoughts, or let us know if you have any other tech challenges you'd like us to tackle next. We love hearing from you and helping you on your tech journey!

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