0

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 ?

1 Answer 1

1

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();
  }
};
Sign up to request clarification or add additional context in comments.

2 Comments

but i have a question, can you explain the concept of what you did ?
I am storing the previous state in a variable. So I am able to respect this value, when I determine which action to call (modify/save). In this example I've changed the variable from true to false and the otherway around. You can also achieve this with a single line of code: $scope.alreadyModified = !$scope.alreadyModified Because we have a boolean (true/false) here, we can just negate (with leading !) the previous value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.