简体   繁体   中英

Why track by $index is necessary for ng-click to trigger inside nested ng-repeat

I was using ng-click inside the nested ng-repeat but found that the inner most ng-click(3rd level) was not getting triggered. But when I used track by $index for the inner most ng-repeat, ng-click got triggered. I am not able to understand this behavior of nested ng-repeat.

Please find below the code snippet

<ul class="a">
    <li class="b" ng-repeat="items in Objects track by $index" ng-click="callSomething()">
        <a>{{items.name}}</a>
        <ul class="c">
            <li class="d" ng-repeat="items1 in Objects1 track by $index" ng-click="callSomething()">
                <a>{{items1.name}}</a>
                <ul class="e">
                    <li class="f" ng-repeat="items2 in Objects3 track by $index" ng-click="callSomething()">
                        <a>{{items3.name}}</a>
                    </li>
                </ul>
            </li>
        </ul>
    </li>
</ul>

Its really related to ng-repeat behavior. Each element is tracked uniquely and mapped to the scope. I think this thread already answers your questions:

How does ng-repeat work?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM