【问题标题】:checked the all checkboxes of form in angularjs检查了angularjs中表单的所有复选框
【发布时间】:2018-03-02 13:13:44
【问题描述】:

嗨,我在 angularjs 中有表单,并且该表单在单击第一个复选框时有很多复选框,即选中所有我想选中的所有复选框。我怎么能用角度为每个字段循环做到这一点这是我在代码片段中尝试的示例代码。

vm.asas = function() {
  angular.forEach($scope.quizSettingsForm, function(field) {
    console.log(field);
    angular.forEach(field, function(errorField) {
      errorField.$setTouched();
    });
  });
  console.log(vm.QuizSettings);
};
<form name="quizSettingsForm" novalidate>
  <div class="panel panel-default">
    <div class="panel-heading">
      QUIZ SETTINGS
    </div>
    <div class="panel-body">
      <ul>
        <li>
          <div class="checkbox">
            <input type="checkbox" id="checkall" ng-model="vm.checkAll" ng-click="vm.asas()" />
            <label for="checkall">Check /Uncheck ALL</label>
          </div>
        </li>
        <li>
          <div class="checkbox">
            <input type="checkbox" ng-model="vm.QuizSettings.IsOpened" id="Cohort" />
            <label for="Cohort">Have a open Quiz</label>
          </div>
        </li>
        <li>
          <div class="checkbox">
            <input type="checkbox" ng-model="vm.QuizSettings.IsMandatory" id="mandatory" />
            <label for="mandatory">Is Mandatory</label>
          </div>
        </li>
        <li>
          <div class="checkbox counterdiv">
            <input type="checkbox" ng-model="vm.QuizSettings.ShouldExpireAfterExpiry" id="invitation" />
            <label for="invitation">  <p>The quiz will expire <input type="number" ng-model="vm.QuizSettings.Expiry" ng-min="1" name="expiry"> days after invitation</p></label>
            <span class="validation-message" ng-show=" vm.QuizSettings.ShouldExpireAfterExpiry && quizSettingsForm.expiry.$touched && quizSettingsForm.expiry.$error.min"><i class="fa fa-warning"></i> The minimum value is 1.</span>
          </div>
        </li>
        <li>
          <div class="checkbox">
            <input type="checkbox" ng-model="vm.QuizSettings.AllowRetakes" id="dontallow" />
            <label for="dontallow">Allow Retake and dont allow more than </label>
            <input type="number" ng-model="vm.QuizSettings.AlllowMoreThen" ng-min="1" name="dontallow"><span style="color: #696969;font-size: 18px;font-weight: normal;"> Retakes </span>
            <span class="validation-message" ng-show="quizSettingsForm.dontallow.$touched && quizSettingsForm.dontallow.$error.min"><i class="fa fa-warning"></i> The minimum value is 1.</span>
          </div>
        </li>
      </ul>
    </div>
  </div>
</form>

【问题讨论】:

    标签: html angularjs forms


    【解决方案1】:

    一种方法(如果复选框属性与其他属性位于同一对象上)

    您可以(不一定应该)将属性名称存储在数组中,然后您可以使用forEach 循环在vm.quizSettings 对象上设置属性本身:

    function selectAll(){
    
      var myArray = [
        "IsOpened", 
        "IsMandatory", 
        "ShouldExpireAfterExpiry"
      ];
    
      angular.forEach(myArray, function(propName){
        vm.QuizSettings[propName] = true;
      });
    }
    

    好多了(如果复选框属性单独在同一个对象上)

    如果您可以避免将属性名称存储为字符串,那就更好了。如果您要更新的所有复选框布尔属性都单独在它自己的对象上,那么它会变得更加清晰:

    function selectAll(){
      var myArray = Object.keys(vm.QuizSettings);
      angular.forEach(myArray, function(propName){
        vm.QuizSettings[propName] = true;
      });
    }
    

    Better Still(只需装饰应应用此行为的复选框)

    创建一个自定义指令selectAllListener,该指令侦听“SelectAll”事件并在引发该事件时将ngModel 值更新为true

    function thisDirective() {
      return {
        restrict: 'A',
        require: 'ngModel',
        link: function(scope, el, attr, ngModel){
          scope.$on("SelectAll", function(){
            ngModel.$setViewValue(true);
            ngModel.$render();  
          });
        }
      }
    }
    

    select-all-listener 装饰器指令添加到您的复选框:

    <li>
      <div class="checkbox" >
        <input select-all-listener type="checkbox" id="checkall" ng-model="vm.checkAll" ng-click="vm.asas()"/>
        <label for="checkall" >Check /Uncheck ALL</label>
      </div> 
    </li>
    

    从控制器中引发事件:

    function selectAll(){
      $rootScope.$broadcast("SelectAll");
    }
    

    【讨论】:

    • 我可以用我的问题代码中的表单字段来做吗
    • 如果 vm.QuizSettings 在属性旁边也有一些 obj 怎么办
    • @Asad 这是第一个答案的假设,将属性名称存储在数组中,这样您就不会覆盖 vm.QuizSettings 对象上的其他属性/对象。
    • 那很麻烦,我想我可以用 angular.foreach 字段循环吗?
    • @Asad 您可以改用angular.forEach,它的工作方式相同,只是语法不同。我已经更新了答案,改为使用angular.forEach()
    【解决方案2】:

    为您的表单构建一个对象,其中 isChecked 键是指复选框值,名称是指标签,值是指输入的模型或值。

    $scope.FormAttributeList = [
        {
            id: 1,
            name: "xyz",
            value: "xyz",
            isChecked: false
        },
        {
            id: 2,
            name: "xyz",
            value: "xyz",
            isChecked: false
        },
        {
            id: 3,
            name: "xyz",
            value: "xyz",
            isChecked: false
        },
        {
            id: 4,
            name: "xyz",
            value: "xyz",
            isChecked: false
        }
    ]
    
    your code will be like 
    
    <div class="panel-body">
        <ul>
            <li>
               <div class="checkbox" >
                    <input type="checkbox" id="checkall" ng-model="vm.checkAll" ng-click="selectAll(vm.checkAll,FormAttributeList)"/>
                    <label for="checkall" >Check /Uncheck ALL</label>
                </div> 
            </li>
            <li ng-repeat="item in FormAttributeList">
                <div class="checkbox">
                    <input type="checkbox" ng-model="item.value" id="item.id" />
                    <label for="Cohort" >{{item.name}}</label>
                </div>
            </li>
    </div>
    
    Now create a function like 
    
    $scope.selectAll = function(checkAll,List) {
        angular.forEach(list,function(value,key){
            value.isChecked = checkAll;
        });
    };
    

    这是做你想做的最好的方法,它会删除你不必要的代码。

    【讨论】:

    • 这一直有效,直到您到达 ShouldExpireAfterExpiry,那里还有其他字段,例如一个绑定到vm.QuizSettings.AlllowMoreThen 的文本框,带有它自己的验证标准和消息。
    猜你喜欢
    • 2017-05-31
    • 2018-03-27
    • 2013-05-08
    • 2015-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-10
    相关资源
    最近更新 更多