【问题标题】:How to set selected checkbox in checkbox list based on value in angularJs with MVC如何根据带有MVC的angularJs中的值在复选框列表中设置选中的复选框
【发布时间】:2017-05-27 08:07:16
【问题描述】:

我想在复选框列表中将“写作”、“足球”等少数爱好设置为选定的爱好,该怎么做? 我的代码如下,

角度控制器

$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>

【问题讨论】:

    标签: angularjs asp.net-mvc checkboxlist


    【解决方案1】:

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

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

    然后像这样在你的视图中绑定它:

    <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>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-25
      • 1970-01-01
      • 2011-10-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多