如何复选框选中默认的单个和多个复选框? ng-init="checkboxModel.value=true" 它不工作

Posted

技术标签:

【中文标题】如何复选框选中默认的单个和多个复选框? ng-init="checkboxModel.value=true" 它不工作【英文标题】:How to checkbox checked default single and multiple checkbox? ng-init="checkboxModel.value=true" its not working 【发布时间】:2020-10-08 07:03:39 【问题描述】:
<input type="checkbox" ng-model="checkboxModel.value" ng-true-value="'true'" ng-false-value="'false'" ng-click='checkboxClick(checkboxModel.value, index);' ng-init="checkboxModel.value=true" />

【问题讨论】:

【参考方案1】:

var demoApp = angular.module('myApp', []);
demoApp.controller('languageController', function($scope, $http) 

    $scope.languages = [
      id:1, name:'C#',
      id:2, name:'MVC',
      id:3, name:'SQL',
      id:4, name:'AngularJs',
      id:5, name:'jQuery',
      id:6, name:'javascript',
      id:7, name:'html',
      id:8, name:'CSS',
      id:9, name:'Linq',
      id:10, name:'EntityFramework'
    ];
    
    $scope.defaultSelected = [
      id:2, name:'MVC',
      id:4, name:'AngularJs'
    ];
    
    $scope.checkID = function(name)
      var dat = $scope.defaultSelected.find(v=> v.name === name);
      if(dat) return true else return false
    

    $scope.submit = function() 
        $scope.result = $scope.loopData;
    ;

);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>


<body ng-app="myApp">
        <div class="wrapper wrapper-content middlealigen col-sm-12" ng-controller="languageController">
            
            <div ng-repeat="language in languages">
                
                <input type="checkbox" ng-model="language.checked" id="language$index" ng-checked="checkID(language.name)" />
                
                <label for="language$index" ng-bind="language.name"></label>
            </div>

        </div>
    </body>

【讨论】:

以上是关于如何复选框选中默认的单个和多个复选框? ng-init="checkboxModel.value=true" 它不工作的主要内容,如果未能解决你的问题,请参考以下文章

我有多个复选框和多个文本输入。如果复选框被选中,则需要启用它旁边的文本输入

选中多个复选框时如何显示div?

如何判断复选框 checkbox 是选中状态还是未选中

jQuery查看是不是选中了任何复选框

下一个元素中的 jQuery 计数选中复选框

delphi 如何判断多个checkbox是不是选中