search

Home  >  Q&A  >  body text

angular.js - ngrepeat adds attributes to specified content

    <p ng-repeat="program in programs">
        <a class="pBlock" href="#"></a>
    </p>

There are 10 pieces of data in programs. Now I want to add a specific attribute command rightCtrl
to the a tag of the 5th and 10th items to achieve the following effect

    <p><a class="pBlock" href="#"></a></p>
    <p><a class="pBlock" href="#"></a></p>
    <p><a class="pBlock" href="#"></a></p>
    <p><a class="pBlock" href="#"></a></p>
    <p><a class="pBlock" href="#" rightCtrl></a></p>
    <p><a class="pBlock" href="#"></a></p>
    <p><a class="pBlock" href="#"></a></p>
    <p><a class="pBlock" href="#"></a></p>
    <p><a class="pBlock" href="#"></a></p>
    <p><a class="pBlock" href="#" rightCtrl></a></p>

How to achieve this

phpcn_u1582phpcn_u15822812 days ago681

reply all(3)I'll reply

  • 淡淡烟草味

    淡淡烟草味2017-05-15 16:56:31

    ng-repeat指令会遍历programs集合,然后给集合中的每一个元素都生成一个模板实例,集合programs中的每个元素都会被赋予自己的作用域和模板,同时每个每个模板实例的作用域都会暴露出一些属性。
    你可以利用其中的$indexProperties to accomplish what you want.

    I have an example here. You can take a look at it, determine which one of the instructions you want to add, and then write your code. demo

    You may have to go through a firewall to view the code. If you can’t browse it, I can take a screenshot for you.

    reply
    0
  • 过去多啦不再A梦

    过去多啦不再A梦2017-05-15 16:56:31

    <p ng-repeat="program in programs">
        <a class="pBlock" href="#" ng-if='$index == 4' rightCtrl></a>
        <a class="pBlock" href="#" ng-if='$index != 4'></a>
    </p>

    reply
    0
  • 过去多啦不再A梦

    过去多啦不再A梦2017-05-15 16:56:31

    Use ng-if directive.

    reply
    0
  • Cancelreply