Monitoring ng-repeat completion after inserting rows within the middle using AngularJS

Many solutions suggest using the scope.$last value to detect when ng-repeat finishes, such as:

angular.module('fooApp')

    .directive('onLastRepeat', function () {
        return function (scope, element, attrs) {
            if (scope.$last) {
                setTimeout(function () {
                    scope.$emit('onRepeatLast', element, attrs);
                }, 1);
            }
        };
    })

The drawback of this solution is that it only works when appending data at the end of the list. If you insert data in the middle, scope.$last will always be false and there is no way to know when angularjs has finished rendering the newly bound rows. Is there a solution that works regardless of where ng-repeat renders the data?

For example:

html:

<button ng-click="addToList()"></button>
<table>
    <tbody>
        <tr ng-repeat="foo in list" on-last-repeat><td><span>{{foo}}</span></td></tr>
    </tbody>
</table>

controller code:

$scope.addToList = function() {
    $scope.list.splice(1, 0, 4);
}

$scope.list = [1, 2, 3];

If you click the button in an example with the above code, scope.$last remains false in the directive intended to trigger when rendering is complete.

Answer №1

Would this snippet of code be useful for your project?

angular.module('fooApp')
.directive('onLastRepeat', function () {
    return function (scope, element, attrs) {
        if (scope.$middle) {
            setTimeout(function () {
                scope.$emit('onRepeatMiddle', element, attrs);
            }, 1);
        }

        if (scope.$last) {
            setTimeout(function () {
                scope.$emit('onRepeatLast', element, attrs);
            }, 1);
        }
    };
})

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

There seems to be a problem as exits.success is not a recognized function -

Currently, I am exploring how to utilize Sails.js with the node-machine-syntax specifically in the context of listing flights. The code snippet below outlines my attempt at achieving this: /** * FlightsController * * @description :: Server-side actions fo ...

md-datepicker incorrect input- AngularJS

When selecting a non-existent date manually in the input, the calendar shows the date I expect but the input does not. Here is the code snippet: <md-datepicker name="startDate" class="custom-datepicker" ng-model="models.startDate" md-placeholder="Ente ...

Add up unique values in MongoDB

Document layout { "_id": "5a21c5dff772de0555480ef5", "date": "2017-10-06T00:00:00.000Z", "amount": 1, "location": "Canada", "__v": 0 } I am looking to calculate the total amount for each location and presen ...

Tips on utilizing JavaScript to retrieve all HTML elements that have text within them, then eliminating the designated element and its descendants

Simply put, I am looking to extract all the text-containing elements from the HTML and exclude specific elements like 'pre' or 'script' tags along with their children. I came across information suggesting that querySelectorAll is n ...

Attempting to retrieve blog posts through an API call but receiving an error message indicating that the

In the blog application I developed, the home page is supposed to display blog posts from all authors with images of the posts and their profile pictures. However, I am encountering an issue where the images are not being displayed due to a (failed)net::ER ...

Customizing the value in a dynamically loaded Ext JS DisplayField

During the runtime of my program, I have a DisplayField that dynamically loads with a date value in the format 'Ymd' (without separators). What I am trying to achieve is changing the format of the date to 'd.m.Y' after it has been loade ...

How to implement pagination with subdocuments in Node.js using Mongoose

My attempt to paginate a subcomment named "Items" within a collection using mongoose-paginate-v2 has not been successful. Below is my model: const mongoosePaginate = require('mongoose-paginate-v2'); const PettyCashItemsSchema = ...

Struggling to manage the flow of my program, constantly switching back and forth between handling AJAX callbacks and rendering PIXI

I'm struggling with optimizing the flow of my JavaScript script. The script is responsible for receiving and sending mouse coordinates to be drawn. Take a look at the code snippet below: //Initializing PIXI var stage = new PIXI.Stage(0x000000); var ...

JavaScript can be used to enable or disable a text box within a GridView when a checkbox is changed

I have a GridView on a webpage containing a Textbox and a CheckBox. I want the Textbox to be enabled when the CheckBox is checked, and disabled when it is unchecked using pure javascript. Can anyone help me solve this issue? Thank you in advance. <asp ...

AngularJS throws an error message stating "Unknown Provider for angular when attempting to create a

My current filter ($index % 4 == 0) seems to be causing an issue where I am getting the error message: Error: [$injector:unpr] Unknown provider: (FilterProvider <- (Filter Inquiry: How can I modify this filter so that I can successfully create a wrapp ...

When Buttons Decide to Take a Break: A Guide to What Happens After You Click Them

Looking for some assistance with my code! I have a set of two buttons and when the user clicks on either one, it triggers a function (which is working perfectly). However, if the user clicks again on the same button, I want it to do nothing. Here is my HT ...

What causes the "Unknown provider: defaultFilterProvider <- defaultFilter" error in AngularJS when using angular-filters?

After incorporating angular-filters into my application.js, I am encountering the following error: Unknown provider: defaultFilterProvider <- defaultFilter Can someone advise on how to resolve this issue? ...

Issue with AngularJS directive's ng-show not toggling as expected when variable changes

I am in the process of developing a straightforward HTML5 video playlist application. I have set up an overlay div that should show/hide when the video is paused or played. Although I have used ng-show and a variable to control it, I am not seeing any cha ...

I keep receiving a blank request body while using express.js

I'm having trouble retrieving data from my database as the body of the request is empty. I've tried using parse-body and CORS, but it's still not working. I attempted various solutions without success. Backend code: const bodyParser = requir ...

Adding mCustomScrollbar to a dynamically created div using javascript steps

When making an ajax request, I retrieve data from the database and use JavaScript to generate HTML. However, there is an issue where the CatItemDetails div sometimes becomes too large and requires a scroll bar to display all the content. I attempted to ad ...

Getting started with Tween JS on a three-dimensional cube in Three JS

I'm venturing into the world of Tween JS and attempting to create a basic animation that moves objects to the right using Tween. Here is the code snippet from my init function (utilizing Three JS): var geometry = new THREE.CylinderGeometry(200, 200, ...

Trouble with formatting credit card numbers in Vue.js

My payment gateway component includes a feature where selecting credit card triggers the _formatCreditCard method to format the credit card number like this: 4444 2442 4342 3434 This is the function in question: _formatCreditCard: function() { var n ...

What is the best way to retrieve a specific key from a JavaScript Map containing an array?

I am currently iterating through a two-dimensional array to populate a map. The map is using the [i,j] indices as keys and the corresponding arr[i][j] values as values: const arrMap = new Map() for(let i = 0; i < arr.length; i++){ for(let j = 0 ...

Opening the Gmail app from a link using JavaScript

What is the best way to open the Gmail app from a hyperlink? This link opens WhatsApp <a href="https://wa.me/">whatsapp</a> <a href="mailto:<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6a1f190f ...

Tips for sending a JavaScript variable through an AJAX URL

I currently have a variable defined like this: var myip; I need to include this variable in the following URL: $.ajax('http://api.ipstack.com/**[myip]**?access_key=mykey') Manually replacing [myip] with my IP address works fine, but I am loo ...