angularjs - 如何使用 MVC 根据 angularJs 中的值在复选框列表中设置选定的复选框

标签 angularjs asp.net-mvc checkboxlist

我想在复选框列表中设置为选定的一些爱好,例如“写作”、“足球”,如何做到这一点? 我的代码如下,

Angular Controller

$scope.Hobbies = [];
$scope.Hobbies.push('Cricket');
$scope.Hobbies.push('Reading');
$scope.Hobbies.push('Writing');
$scope.Hobbies.push('Sleeping');
$scope.Hobbies.push('Running');
$scope.Hobbies.push('Football');
$scope.Hobbies.push('Programming');

$scope.selected = {};    
$scope.GetSelected = function () {        
    console.log('selected arry....');       
    $.each($scope.selected, function (value,checked) {
        console.log(value + '-' + checked);
    });
};

 $scope.SetSelected = function () {        
   //???
};

我的.cshtml代码

 <div><b>Hobbies</b>
                        <label ng-repeat="hobbie in Hobbies">                                
                            <input type="checkbox"
                                   checklist-model="Hobbies"
                                   ng-model="selected[hobbie]"                                      
                                   checklist-value="{{hobbie}}">{{hobbie}}                               
                        </label>
                    </div>
<a href="javascript:;" data-ng-click="SetSelected();">Set Selected</a>

最佳答案

您可以像这样定义您的列表。

$scope.Hobbies = [
     {name: 'Cricket', checked: true}, 
     {name: 'Reading', checked: false},
     {name: 'Writing'}
   ];

然后将其绑定(bind)到您的 View 中,如下所示:

<label ng-repeat="hobbie in Hobbies">                                
  <input type="checkbox"  ng-model="hobbie.checked">{{hobbie.name}}
</label>

要获得选定的爱好,请使用

$scope.GetSelected = function () {        
    $scope.Hobbies.forEach(function (hobbie) {
        console.log(hobbie.name,  hobbie.checked);
    });
};

供引用的工作解决方案:

angular.module('myApp',[])
.controller('Controller', function($scope) {
   $scope.Hobbies = [
     {name: 'Cricket', checked: true}, 
     {name: 'Reading', checked: false},
     {name: 'Writing'}
   ];
   
$scope.GetSelected = function () {        
    $scope.Hobbies.forEach(function (hobbie) {
        console.log(hobbie.name,  hobbie.checked);
    });
};

})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="myApp" ng-cloak ng-controller="Controller">
  <div><b>Hobbies</b>
    <label ng-repeat="hobbie in Hobbies">                                
                            <input type="checkbox"
                                   ng-model="hobbie.checked">{{hobbie.name}}                               
                        </label>
  </div>
  <a href="javascript:;" data-ng-click="GetSelected();">Get Selected</a>
</body>

关于angularjs - 如何使用 MVC 根据 angularJs 中的值在复选框列表中设置选定的复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44214499/

相关文章:

javascript - angular $监视数组中所有对象的特定字段

angularjs - Karma 测试运行器和闭包库 "NOT SERVED FILE"

asp.net-mvc - 如何在单击按钮时重新绑定(bind) igGrid igniteUI 控件中的数据?

c# - MVC4 中 Global.asax.cs 页面中的问题

asp.net-mvc - Asp .Net MVC Viewmodel 应该是类还是结构?

c# - 用数据库中的项目填充复选框列表?

asp.net - 从复选框列表在 VB.net 中创建 SQL 查询

angularjs - columnFilter 插件不适用于 Angular DataTables 服务器端处理

javascript - 将ng-disabled参数设置为$ ionicPopup中的button

winforms - 以编程方式选择 winforms 复选框列表中的项目