2014-08-31 52 views
0

如何在當前值更改之前識別「單選按鈕」的值更改?angularjs:更改單選按鈕值之前的事件

在我的應用程序中,我需要提醒用戶有關更改此單選按鈕的值將導致其餘形式的影響。

我已經嘗試過使用ngChange和ngClick,但單選按鈕的值始終會更改(到新值),即使在我可以使用當前值執行某些操作之前。

例子:

 <form> 
     <label>Nivel de gestão</label> 
     <input name="gestao" type="radio" data-ng-model="nivelGestao" value="T" data-ng-change="mudarNivelGestao()">Tecnica 
     <input name="gestao" type="radio" data-ng-model="nivelGestao" value="O" data-ng-change="mudarNivelGestao()">Operacional 
     <input name="gestao" type="radio" data-ng-model="nivelGestao" value="I" data-ng-change="mudarNivelGestao()">Institucional 

     <table> 
      <tr> 
       <td>Id</td> 
       <td>Nome</td> 
      </tr> 
      <tr data-ng-repeat="gestor in gestores"> 
       <td>{{gestor.id}}</td> 
       <td>{{gestor.nome}}</td> 
      </tr> 
     </table> 

<script> 
... 
    $scope.mudarNivelGestao = function() { 
     alert($scope.nivelGestao); // In this place the content has already been changed, but before I need to alert and ask if the User want continue, if "No" return to the last value, if "Yes" change the value and go ahead... 
     ... 
    } 
... 
</script> 


    </form> 
+0

'$範圍。$表( 'nivelGestao',函數(的newval,OLDVAL,ATTR){ 如果(用戶需要改變){}其他{$ scope.nivelGestao = OLDVAL}} )'你可以像這樣看待價值 – 2014-08-31 15:58:18

回答

0

我回答了類似的問題here

就與周圍的單選輸入和標籤NG-單擊事件應用確認邏輯。

<label ng-click="confirmChange($event)"><input type="radio" ng-model="value" value="foo" ></label> 

$scope.confirmChange = function(event) { 
    if(!confirm('sure?')) { 
    event.preventDefault(); 
    } 
}; 
0

可以使用mousedown事件(這將在clickchange事件之前進行發射)。
例如: -

<input type="checkbox" ng-model="option" 
     ng-mousedown="confirmOption($event)" /> 

$scope.confirmOption = function (evt) { 
    var confirmMsg = 'Are you sure you want to select this ?'; 
    if (!$scope.option) { 
     var confirmed = confirm(confirmMsg); 
     $scope.option = confirmed; 
    } 
}; 

還參見本short demo

相關問題