如何使用 MVC 根据 angularJs 中的值设置复选框列表中的选定复选框
How to set selected checkbox in checkbox list based on value in angularJs with MVC
我想在复选框列表中设置为选中的'Writing'、'Football'等少数爱好如何操作?
我的代码如下,
angular 控制器
$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'}
];
然后像这样在您的视图中绑定它:
<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>
我想在复选框列表中设置为选中的'Writing'、'Football'等少数爱好如何操作? 我的代码如下,
angular 控制器
$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'}
];
然后像这样在您的视图中绑定它:
<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>