2016-03-31 3 views
0

トグルボタンをクリックする必要があります。一度クリックすると合計金額に金額が追加されます。ボタンをもう一度クリックして選択を解除すると、その金額が減算されます合計から。交互に加減算する関数Angularjs

私が選択したボタンの背景色を変更するには、クラスの追加と削除されたこのHTML持っている - 「

app.controller('AppointmentController', ['$scope','services', function($scope, services) { 
var data = {}; 
data.fn = 'services'; 
services.getData(data).success(function(return_data){ 
    console.log(return_data); 
    $scope.services_data = return_data; 
}); 
var data = {}; 
data.fn = 'get_barbers'; 
services.getData(data).success(function(rd){ 
    $scope.barbers = rd; 
    console.log(rd); 
}); 
$scope.total_price = 0; 
$scope.time = 0; 
$scope.price = function(price, time){ 
    $scope.total_price = parseInt(price) + parseInt($scope.total_price); 
    $scope.time = parseInt(time) + parseInt($scope.time); 
} 
}]); 

おそらくかなり簡単ですが、私はすることができます -

<div class="col-md-4" ng-repeat="service in services_data track by $index"> 
     <div ng-class="showDetails[$index] ? 'panel-warning': 'panel-default'" class="panel"> 
     <button ng-click="$parent.showDetails[$index] = !$parent.showDetails[$index]; price(service.price, service.est_time_mins)" class="panel-heading btn"><span class="pull-left badge">$ {{service.price}}</span>{{service.name}}</button> 
     <div class="panel-body"> 
     {{service.est_time_mins}} mins. {{service.style}} 
     </div> 
     </div> 
    </div> 

と、このコントローラをそれの周りに私の頭をラップします。ありがとう。

答えて

0

ブールフラグを使用します。

$scope.selected = false; 
$scope.add = function(price) { 
    //If item is currently selected, subtract price 
    if ($scope.selected) $scope.total_price -= price; 
    //If item is currently NOT selected, add price 
    else $scope.total_price += price; 
    $scope.selected = !$scope.selected; 
}; 
関連する問題