AngularJs - Best-Practices on adding an active class on click (ng-repeat)

后端 未结 4 2129
一个人的身影
一个人的身影 2020-12-02 12:32

I want to add an active class on click in a list, i tried the following code, but it adds the active class on all my items :/ :

HTML :



        
4条回答
  •  一向
    一向 (楼主)
    2020-12-02 12:50

    I'm well late to the party, but this is the most reusable pattern, and it will save you re-writing the same function every time.

    HTML:

    JS:

    $scope.list = [{}, {}];
    
    $scope.setActive = function(item, list){
      list.some(function(item){
        if(item.active){
          return item.active = false;
        }
      });
      item.active = true;
    };
    

提交回复
热议问题