Angular instruction Monitor Ng-repeat render complete

Source: Internet
Author: User


At work today, when you need to perform an operation using the Ng-repeat traversal rendering, the angular itself does not provide instructions for listening ng-repeat rendering completion, so you need to create your own custom directives.

Some special properties are exposed within the Ng-repeat template instance $index/$first/$middle/$last/$odd/$even, $index is incremented with each traversal (starting at 0), and the value of the $last is true when traversing to the last. So you can monitor the execution state of ng-repeat by judging the value of $last,

How to get the value of $last in the traversal process: custom directives

var app = Angular.module (' app ', []);

App.directive (' Repeatfinish ', function () {return
    {
        link:function (scope,element,attr) {
            Console.log ( Scope. $index)
            if (scope. $last = = true) {
                scope. $eval (Attr.repeatfinish);}}}
)

App.controller (' Appctrl ', function ($scope, $element) {
    $scope. arr = [1,2,3,4,5,6];
    $scope. tip = ';

Defines the method to be executed after the repeat is completed, and the method name can be defined $scope. Repeatdone = function () { $scope. tip = ' Ng-repeat finished, I'm going to start doing something ... ';
Execute the event method you want to execute } });

It is OK to invoke the instruction using angular when calling.

<div ng-repeat= "I in Arr" repeat-finish= "Repeatdone ();" >
    <p ng-bind= "i" ></p>
</div>

Demo


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.