角度js中的多个复选框验证

时间:2015-06-17 11:06:10

标签: javascript angularjs

libraryApp.directive("checkboxGroup", function() {
        return {
            restrict: "A",
            link: function(scope, elem, attrs) {
				
                // Determine initial checked boxes
                if (scope.groupArray.indexOf(scope.item.id) !== -1) {
                    elem[0].checked = true;
                }

                // Update array on click
                elem.bind('click', function() {
                    var index = scope.groupArray.indexOf(scope.item.id);
                    // Add if checked
                    if (elem[0].checked) {
                        if (index === -1) scope.groupArray.push(scope.item.id);
                    }
                    // Remove if unchecked
                    else {
                        if (index !== -1) scope.groupArray.splice(index, 1);
                    }
                    // Sort and update DOM display
                    scope.$apply(scope.groupArray.sort(function(a, b) {
                        return a - b
                    }));
                });
            }
        }
    });
angular js and bootstrap
<form class="userForm" name="userForm" novalidate ng-init="">
	<notifications></notifications>
	<section>
	<div class="row">
		<div class="col-md-11">	
          <div class="form-group" style="display: flex;" ng-class="{ 'has-error' :(userForm.status.$dirty && userForm.status.$error.required) alert(ram)}" >
				<label class="assetDropdownLabel col-md-2" for="manufacturer">Permission<span class="text-danger">*</span></label>
				
                <div ng-repeat="item in list"  id="status" name="status" ng-model="status" required ng-required="true" >
				
					<label style="margin-right:12px"><input type="checkbox" style="display:inline;  width: 18px;margin:0px" checkbox-group required  />{{item.value}}</label>
					
				</div>				
				<div ng-show="userForm.status.$dirty && userForm.status.$error.required" class="help-block col-md-offset-2">Required field</div>
		
	</div>
	</div>
	</div>
	</section>

	<div class="btnContainer addUserBtn" >
		<button type="button" ng-click="finalValidation()" class="pull-right primarybutton btn_small addmargintopbottom" alt="{{buttonTitle}}" ng-disabled="userForm.$error.pattern">{{buttonTitle}}</button>
	</div>
</form>

我想检查所需复选框的验证;需要选择至少一个复选框,然后在通知中不会添加任何消息或ng-class。如果未选中复选框,则应添加finalvalidation()或ng-class

0 个答案:

没有答案