Anes El Gongi
Anes El Gongi

Reputation: 48

AngularJS - call two functions non sequentially with ng-click

I have a Modify Button, when I want to click on it, the modify() function will be executed, and the text button will change to save, and I click on it, the save() function will be executed.

these are the codes

<button class="btn btn-primary" ng-click="modify(); save()" ng-disabled="!modifyDisabled">{{button}}</button>

$scope.button="Modifier"
$scope.modify=function(){
  $scope.button="Enregistrer"
  $http.get("http://localhost:5000/settings").then(function(response){
    $scope.settingsInserted = false 
    $scope.nbOperateurPresents= response.data.data[0].nbOperateurPresents
    $scope.targetAmount1stHour= response.data.data[0].targetAmount1stHour
    $scope.targetAmount2ndHour= response.data.data[0].targetAmount2ndHour
    $scope.targetAmount3rdHour= response.data.data[0].targetAmount3rdHour
    $scope.targetAmount4thHour= response.data.data[0].targetAmount4thHour
    $scope.targetAmount5thHour= response.data.data[0].targetAmount5thHour
    $scope.targetAmount6thHour= response.data.data[0].targetAmount6thHour
    $scope.targetAmount7thHour= response.data.data[0].targetAmount7thHour
    $scope.targetAmount8thHour= response.data.data[0].targetAmount8thHour
   })  
}

$scope.save=function(){
 console.log('saved')
 $scope.button="Modifier"
}

I want to execute for the first click modifiy(), and in the second click save().

I think that I should use a third function, but I don't know how !

Who can help me ?

Upvotes: 0

Views: 73

Answers (1)

Armin
Armin

Reputation: 15958

You are right, you need a third function in your controller, which toggles between modify and save. Should be pretty easy:

$scope.alreadyModified = false;

$scope.modifyFirstAndThenSave = function() {
  if (!$scope.alreadyModified) {
    $scope.alreadyModified = true;
    $scope.modify();
  } else {
    $scope.alreadyModified = false; // just in case a third click should modify again
    $scope.save();
  }
};

Upvotes: 1

Related Questions