2017-12-29 17 views
0

ng-repeat内に10個のボタンがあり、基本的に複数のデータを取得するスイッチのように動作し、各ボタンはコントローラに値を渡す必要があります。最初のクリック時に配列に追加して、次にその値を削除します(トグルアクション)。私は、すべてのボタンがアクティブになり、ボタンをクリックしたときにng-repeatの中の角度の切り替え方法

私のHTMLコードセグメント

$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 
      } 
     }; 

問題直面

<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> 

コントローラの機能があり、私はvariable0,variable1,variable2などの各ボタンのdiffernt変数を追加してみました。.. (variable{{ambutton}})を追加してコントローラでif-elseifを使用しても問題ありませんが、もっと良い解決策が必要なのは、それぞれのボタンに関連するIDで可能ですか?

答えて

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> 
+0

これは素晴らしいです、しかし、私のユースケースでは、私はこのような他の選択のための入力を取るためにこのボタンのコレクションを再度繰り返す必要がありますhttp://jsfiddle.net/rahulaunni/fv6Lea7j/2/、どうすればそれを解決することができます –

+1

このhttp://をチェックしてくださいj sfiddle.net/fv6Lea7j/4/それはできますか? –

+0

クールに感謝します –

関連する問題