angularjs:更改单选按钮值之前的事件

时间:2014-08-31 15:14:32

标签: angularjs events radio-button

如何识别"单选按钮"的价值变化?在当前价值变化之前?

在我的应用程序中,我需要提醒用户有关更改此单选按钮值将导致其余格式的影响。

我尝试过使用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>

2 个答案:

答案 0 :(得分:0)

您可以使用mousedown事件(在clickchange事件之前触发)。
E.g:

<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

答案 1 :(得分:0)

我回答了类似的问题here

用标签围绕无线电输入,并在标签ng-click事件中应用确认逻辑。

j