AngularJS - 使用ng-click非顺序调用两个函数

时间:2017-09-01 23:00:42

标签: javascript angularjs

我有一个修改按钮,当我想点击它时,将执行 modify()功能,文本按钮将变为保存,然后点击它, save()功能将被执行。

这些是代码

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

我想执行第一次点击修改(),然后在第二次点击保存()

我认为我应该使用第三个功能,但我不知道如何!

谁能帮帮我?

1 个答案:

答案 0 :(得分:1)

你是对的,你需要控制器中的第三个功能,它在修改和保存之间切换。应该很简单:

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