search

Home  >  Q&A  >  body text

angular.js - angular ng-repeat changes the background color of a certain


As shown in the picture, the above 5 blocks are output in a loop using ng-repeat. How to realize that when a certain block (such as block 2) is clicked, the background of the block Color changes to red? The colors of other blocks remain unchanged.
Thanks for the advice.

为情所困为情所困2831 days ago848

reply all(4)I'll reply

  • 大家讲道理

    大家讲道理2017-05-15 17:00:27

    There are too many methods, ng-click, ng-class, directive can be implemented.
    This is a direct method, for reference only
    html:

    <p>
        <span ng-class="{'selected':selected==s}" ng-repeat="s in list" ng-bind="s" ng-click="changeStatus(s)"></span>
    </p>

    js

    $scope.list = [1,2,3,4,5];
    $scope.changeStatus = function(index){
        $scope.selected = index;
    }
    

    reply
    0
  • 黄舟

    黄舟2017-05-15 17:00:27

    Add click event. Pass the $index and $event of the current loop. Then it's OK to handle it yourself through angular jq.

    reply
    0
  • phpcn_u1582

    phpcn_u15822017-05-15 17:00:27

    Give you a ready-made code:
    HTML

    <p class='options'>
        <span class='option' ng-class="{'selected':s._selected}" ng-repeat="s in orderStatus" ng-bind="s.l" ng-click="clickStatus(s)"></span>
    </p>

    JS

    $scope.clickStatus = function(prop) {
        prop._selected = !prop._selected;
    };

    CSS

    .option{display:inline-block;border:1px solid green;padding:.25em;margin:.5em .5em 0 0;}
    .option.selected{background:green;color:white;}

    I am planning to write a directive to do this

    reply
    0
  • 世界只因有你

    世界只因有你2017-05-15 17:00:27

    http://runjs.cn/detail/yfnwk6ho

    reply
    0
  • Cancelreply