使用 Angular 要求输入启用注册:需要输入/验证

Using Angular require on input to enable signup: require input's/ validation

本文关键字:输入 验证 启用 Angular 使用 注册      更新时间:2023-09-26

我有两个输入(复选框)。一个是给学生的,另一个是给顾问的。我想要求至少一个或其他输入复选框。如果点击一个,我让他们切换。我只是不知道如何让它与表单验证的其余部分内联运行。

学生:

    <input type="checkbox" class="userTypeClass" checked="checked" id="studentCheckbox" name="studentCheckbox" ng-true-value="1" ng-model="user.userType" ng-change="inputCheck(user)"/>

顾问:

<input type="checkbox" class="userTypeClass" id="consultantCheckBox" name="consultantCheckBox" ng-true-value="2" ng-model="user.userType" ng-change="inputCheck(user)"/>

注册按钮:

<button class="col button-green" ng-disabled="signupForm.$invalid && !flag" ng-click="submit(user)">Continue</button>

JS控制器功能:

                $scope.inputCheck = function(user) {
        var flag = false;
        if (user.userType == false) {
            flag = true;
        } else {
            flag = false;
        }
        console.log(flag);
    };

可能有更好的答案,但这里有一个想法——

<input type="checkbox" class="userTypeClass" checked="checked" id="studentCheckbox" name="studentCheckbox" ng-true-value="1" ng-model="user.userType" ng-change="inputCheck(user)"/>
<input type="checkbox" class="userTypeClass" id="consultantCheckBox" name="consultantCheckBox" ng-true-value="2" ng-model="user.userType" ng-change="inputCheck(user)"/>
<input type="hidden" required ng-model="user.userType"/>

通过隐藏的输入,您可以确保至少检查一个。我不知道您的输入检查方法的目的,但这里有一种方法可以清理-

$scope.inputCheck = function(user) {
  var flag = user.userType === false;
  console.log(flag);
};
// or if you dont need to store the flag:
$scope.inputCheck = function(user) {
  console.log(user.userType === false);
};

编辑:

嗯,我不知道假是行不通的。你可以尝试一个隐藏的复选框-(未经测试,不确定这是否有效)

<input type="checkbox" ng-hide="true" required ng-model="user.userType"/>

或者您可以在inputCheck中取消设置该值,但它有点笨拙。

$scope.inputCheck = function(user) {
  if(user.userType === false) {
    user.userType = undefined;
  }
};