2014-08-31 124 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

相关问题