ng-repeat中切换md按钮的角度方式

问题描述 投票:0回答:1

我在ng-repeat中有10个按钮,它们基本上像一个开关一样操作以获取多个数据,每个按钮必须将一个值传递给控制器​​,并且在控制器中,在第一次单击时将该值推送到一个数组并在下一个中删除该值(切换)行动)。

我的HTML代码段

<md-button id="{{choice.id}}btn{{ambutton}}" ng-repeat="ambutton in ambuttons" ng-click="getTime(choice,ambutton);" class="md-fab  md-primary" ng-class="{'active md-warn': variable,'disable md-primary': !variable}">{{ambutton}}</md-button>

控制器功能

 $scope.ambutton=[1,2,3,4,5,6,7,8,9,10]
    $scope.getTime = function (choice,ambutton) {
            $scope.variable = !$scope.variable;
            if($scope.variable){
                //save the value to an array;
            }
            else{
              //remove the value from array
             }
        };

面临的问题是当我点击一个按钮时所有按钮变为活动状态,我尝试为每个按钮添加不同的变量,如variable0,variable1,variable2 ...(通过添加variable{{ambutton}})和控制器使用if-elseif它工作正常,但我需要一个更好的解决方案可以用任何可能的id与每个按钮有关?

angularjs angularjs-material
1个回答
0
投票

检查出http://jsfiddle.net/n2p1ocqz/7/

您可以使用variables作为数组并使用索引操作,无论选择与否,如

   $scope.ambuttons=[1,2,3,4,5,6,7,8,9,10]
   $scope.variables=[];
    $scope.getTime = function (choice, ambutton, index) {
            $scope.variables[index] = !$scope.variables[index];
            if($scope.variable[index]){
                //save the value to an array;
            }
            else{
              //remove the value from array
             }
        };

和你的偏爱

<md-button  ng-repeat="ambutton in ambuttons track by $index" ng-click="getTime(choice, ambutton, $index);" class="md-fab  md-primary" ng-class="{'active md-warn': variable,'disable md-primary': !variables[$index]}">{{ambutton}}</md-button>
© www.soinside.com 2019 - 2024. All rights reserved.