Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 不允许取消选中angularJS中的所有复选框_Javascript_Angularjs_Checkbox_Angularjs Directive_Checklist Model - Fatal编程技术网

Javascript 不允许取消选中angularJS中的所有复选框

Javascript 不允许取消选中angularJS中的所有复选框,javascript,angularjs,checkbox,angularjs-directive,checklist-model,Javascript,Angularjs,Checkbox,Angularjs Directive,Checklist Model,我对复选框列表使用AngularJS指令,但我需要始终至少选中一个复选框。 我已通过添加一个if来更改: watch if(current.length > 1) 在删除支票之前: scope.$watch(attrs.ngModel, function(newValue, oldValue) { if (newValue === oldValue) { return; } var current = getter(scope.$parent); if (an

我对复选框列表使用AngularJS指令,但我需要始终至少选中一个复选框。 我已通过添加一个if来更改:

watch if(current.length > 1)
在删除支票之前:

scope.$watch(attrs.ngModel, function(newValue, oldValue) {
  if (newValue === oldValue) { 
    return;
  } 
  var current = getter(scope.$parent);
  if (angular.isFunction(setter)) {
    if (newValue === true) {
      setter(scope.$parent, add(current, value, comparator));
    } else {
        if(current.length > 1)
        {
            setter(scope.$parent, remove(current, value, comparator));
        }
    }
  }

  if (checklistChange) {
    checklistChange(scope);
  }
});
问题是UI已经更改为未检查,而模型没有更改

对于如何将复选框更改回“checked”状态,或者更好地在它更改为checked之前捕获它,任何人都有一个优雅的建议(最好没有JQuery)


JSFIDLE,尝试取消选中all,并看到当所有复选框都未选中时(我试图阻止的),模型不会保持为空(这很好,但还不够)

这可能来自angular的异步特性和您正在使用的库。到目前为止,我可以找到一些类似()的肮脏解决方法:创建一个事件,在其中跟踪输入上的单击并执行以下操作:

  $scope.onclick = function(evt) {
    if ($scope.user.roles.length === 1 && prevLength === 1) {
      evt.preventDefault();
    }

    prevLength = $scope.user.roles.length;
  };
然后将其添加到元素中

 ng-click="onclick($event)"

我不是说它很完美,但它很有效

我的一位同事建议,一旦它是唯一选中的复选框,就禁用它,这样你们都可以防止更改模块(因此指令甚至不需要更改),用户体验更好,因为用户知道他不能取消选中它:

     <input type="checkbox" checklist-model="user.roles" checklist-value="role.id" ng-disabled="shouldDisable(role.id)"> {{role.text}}

 $scope.shouldDisable = function(roleId) {
        var isDisabled = false;
        if($scope.user.roles.length === 1) {
            isDisabled = $scope.user.roles.indexOf(roleId) !== -1;
        }
        return isDisabled;
    }
{{role.text}
$scope.shouldDisable=函数(roleId){
var isDisabled=假;
if($scope.user.roles.length==1){
isDisabled=$scope.user.roles.indexOf(roleId)!=-1;
}
返回被禁用;
}

请参阅JSFIDLE中的答案此时,组件不支持此功能,但讨论了如何实现minlength/maxlength,然后使用检查表minlength=1即可实现此功能


我还编辑了您的建议,以在更改前实施
检查列表
。我会在下一批中尝试处理这个问题。在v0.8.0中实现。

使用数组的@ozba answer的更好版本。某些函数:

public atLeastOneRowEnabled(row): boolean {
        //at least one row is visible except the current line
        return !this.rows.some(rowIterator => rowIterator.logical_name !== row.logical_name && rowIterator.isVisible);
    }

我理解你的问题,但我认为你在寻找错误的解决方案。如果用户不想禁用复选框,就让他这样做。否则,他必须选择另一个复选框,然后才能取消选中旧复选框??您描述的行为是一种糟糕的用户体验。保持简单,如果他取消选中所有复选框并禁用v0.8.0中实现的提交按钮,则使用表单验证显示警告。