Home > Web Front-end > JS Tutorial > Is There an Event Triggered When ng-Repeat Finishes Populating Elements?

Is There an Event Triggered When ng-Repeat Finishes Populating Elements?

DDD
Release: 2024-12-01 07:07:11
Original
824 people have browsed it

Is There an Event Triggered When ng-Repeat Finishes Populating Elements?

ng-Repeat Population Completion Event

Problem

Manipulating elements populated by ng-repeat using jQuery selectors within $(document).ready() or $scope.$on('$viewContentLoaded', myFunc) proves futile. Is there a dedicated event that triggers upon ng-repeat's completion?

Answer

While there is no specific event signaling ng-repeat's termination, utilizing directives and ng-repeat-specific properties provides a viable solution.

Directives

If your goal is to style or attach event handlers to the entire table, you can employ a directive that encapsulates all ng-repeat elements. Conversely, for addressing specific elements, use a directive within ng-repeat, which will execute for each created element.

Ng-Repeat Properties

The $index, $first, $middle, and $last properties enable triggering events. For instance, you can utilize the $last property to display an alert when the last element is created.

Example

Consider the following HTML:

<div ng-controller="Ctrl" my-main-directive>
  <div ng-repeat="thing in things" my-repeat-directive>
    thing {{thing}}
  </div>
</div>
Copy after login

The accompanying directives could resemble:

angular.module('myApp', [])
.directive('myRepeatDirective', function() {
  return function(scope, element, attrs) {
    angular.element(element).css('color','blue');
    if (scope.$last){
      window.alert("im the last!");
    }
  };
})
.directive('myMainDirective', function() {
  return function(scope, element, attrs) {
    angular.element(element).css('border','5px solid red');
  };
});
Copy after login

Sample this solution in action through this Plunker.

The above is the detailed content of Is There an Event Triggered When ng-Repeat Finishes Populating Elements?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template