1

我有以下单选按钮组:

<input type="radio" name="GROUP" ng-model="data1" id="for1" value="value" ng-change="formSubmit()" ng-model-options="{debounce: 3000}">
<input type="radio" name="GROUP" ng-model="data2" id="for2" value="value" ng-change="formSubmit()" ng-model-options="{debounce: 3000}">

如您所见,在 上ng-click,我让它运行一个特定的功能,但也有一个debounce仅在 3 秒超时时发生。

当我ng-model-options="{debounce: 3000}"出现时,我的广播组经常会取消选中——这意味着组中的任何输入都不会被选中。

当我删除去抖动时,不会发生此问题。

有谁知道我该如何解决这个问题?

4

2 回答 2

0

基于上面的评论线程,并假设您想保留第一次点击并在 3 秒延迟内忽略后续点击,我建议:

<input type="radio" ng-change="doItLater();" value="X">

(同时,在指令中:

scope.doItLater = function() {
    if (scope.timeoutwatcher !== undefined) {return;}
    scope.timeoutwatcher = $timeout(function() {
        // do it
        delete scope.timeoutwatcher;
    },3000);
}

或者,如果您希望在超时期限内让稍后的点击覆盖之前的点击,

scope.doItLater = function() {
    $timeout.cancel(scope.timeoutwatcher);
    scope.timeoutwatcher = $timeout(function() {
        // do it
    },3000);
}
于 2015-07-06T20:28:18.037 回答
0

这样的事情可能会有所帮助

angular
    .module('app', [])
    .controller('example', ['$scope', function($scope) {
        $scope.user = {};
        $scope.$watch('user.gender', $scope.callback);
        $scope.callback = function() {
            alert($scope.user);
        }
    }]);

在 html 中,类似

  <form ng-app="app" ng-controller="example">
    <input name="jim" type="radio" ng-model="user.gender" value="male" ng-model-options="{debounce: 3000}" />male
    <input name="jim" type="radio" ng-model="user.gender" value="female" ng-model-options="{debounce: 3000}" />female
    <br />
  <pre>form = {{user | json}}</pre>

于 2015-07-06T20:34:24.030 回答