如何使用Angular绑定复选框列表,并使用NG-REAPET方法从复选框中获取所有选定的值



如何使用AngularJS绑定复选框列表,并使用NG-REAPET方法从复选框中获取所有选定的值?

我尝试在Angular Controller中获取所选复选框的值。

查看

<ul class="to_do">
    <li ng-repeat="form in ManagementScreenModel.Forms">
        <!--<input type="checkbox" ng-model="RoleData.formUrl" value="{{form.Value}}" id="{{form.Value}}">-->
        <input type="checkbox"
               ng-model="RoleData.selectedForms"
               data-checklist-model="RoleData.ManagementScreenModel.Forms"
               data-checklist-value="{{form.Value}}"
               id="{{form.Value}}">
        {{form.Text}}
    </li>
</ul>

控制器

$scope.SetUserpermision = function() {
    Get(
        "/Home/GetAvailableForms",
        null,
        function(result) {},
        function(result) {
            userforms = result;
            //$scope.GetUserForms = json.parse(result);
        },
        null
    );
    Post(
        "/ManagementScreen/SetAccess",
        $scope.RoleData.userforms,
        function OnError(jqXHR, testStatus, errorThrown) {
            // display error here
        },
        function (result) {
            var response = JSON.parse(result);
            if (response != null && response.StatusCode === 200) {
                //alert("User email change successfully");
            }
        },
        null
    );
}

ManagementScreenModel.Forms中具有布尔变量(例如'检查'),并在提交表单检查变量后检查是否检查条目。

html

<md-checkbox ng-model="user.checked" aria-label="Checkbox 1"></md-checkbox>

JS

    angular.forEach($scope.users, function(user, i) {
     if(user.checked === true){
//store in an array
}
});

演示https://plnkr.co/edit/mtydsrxczxljxvhcyrcv?p=preview

var app = angular.module("myapp", []);
app.controller("myCtrl", ['$scope', function($scope) {
  $scope.roles = [
    'Nitin', 
    'Parag', 
    'Pravin', 
    'Sandeep',
    'Praful'
  ];
 $scope.items=[];
   $scope.addOrRemove = function (selectedItems,b) {         
      
     if(b==true)
       $scope.items.push(selectedItems);
     else
       {
       var index = $scope.items.indexOf(selectedItems);
           $scope.items.splice(index,1);
       }
     
      };
  
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myapp">  
  <div ng-controller="myCtrl">
   <label ng-repeat="role in roles">
  <input type="checkbox" ng-model="user.roles"  ng-value="role"  ng-click="addOrRemove(role,!user.roles)" ng-init="user.roles=false;"  > {{role}}
</label>
  
  <br/> <br/> <br/>
    
    {{items}}
  </div>
  
</div>

最新更新