如何在angularJs中同时绑定单选按钮和复选框?

时间:2016-11-18 06:59:38

标签: javascript html angularjs

我试过了,如果我勾选复选框,那么单选按钮也会被选中。当我选中复选框时它会很好。

但是,如果我选择单选按钮绑定过程没有发生。即,如果我单击单选按钮,则必须选中复选框。

我不知道该怎么做?

这里放置了代码。

<!DOCTYPE html>
<html lang="en-US">
<head>
<title>Learn It HTML Template</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular.min.js"></script>
</head>
<body ng-app="sampleApp">
<div ng-controller="sampleController">
Are you going for party tonight: <input type="checkbox" ng-checked="checked" ng-model="checked">  
<br/> <br/>
You should go, Complete this example and rest examples you can learn tomorrow :), So click on the check box above: 
<br/> <br/>
<input id="checkSlave" type="radio" ng-checked="checked">Yeah :)

</div>
<script>
var app = angular.module("sampleApp", []);
app.controller('sampleController', ['$scope', function ($scope) {
$scope.checked= 0;
}]);
</script>
</body>
</html>

有人可以帮忙吗?

4 个答案:

答案 0 :(得分:1)

试试这个

&#13;
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular.min.js"></script>
<script>
var app = angular.module("sampleApp", []);
app.controller('sampleController', ['$scope', function ($scope) {


}]);		
</script>
<body ng-app="sampleApp">
<div ng-controller="sampleController" ng-init="checked='false'">
Are you going for party tonight: <input type="checkbox" ng-checked="checked" ng-click="checked = !checked">  
<br/> <br/>
You should go, Complete this example and rest examples you can learn tomorrow :), So click on the check box above: 
<br/> <br/>
<input id="checkSlave" type="radio" ng-checked="checked" ng-click="checked = !checked">Yeah :)
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

你可以做这样的事情,肯定会有效

<!DOCTYPE html>
<html lang="en-US">
<head>
<title>Learn It HTML Template</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular.min.js"></script>
</head>
<body ng-app="sampleApp">
<div ng-controller="sampleController">
Are you going for party tonight: <input type="checkbox" ng-checked="slave" ng-model="checked">  
<br/> <br/>
You should go, Complete this example and rest examples you can learn tomorrow :), So click on the check box above: 
<br/> <br/>
<input id="checkSlave" type="radio" ng-checked="checked" ng-click="slave=true">Yeah :)

</div>
<script>
var app = angular.module("sampleApp", []);
app.controller('sampleController', ['$scope', function ($scope) {
$scope.checked= false;
$scope.slave=false;
}]);
</script>
</body>
</html>

我已经介绍了另一个变量,它会在单击单选按钮时发生变化,因为从单选按钮触发的事件不是检查事件,而是onclick事件。

答案 2 :(得分:1)

试试这个工作得很好..!

<!DOCTYPE html>
<html lang="en-US">
<head>
<title>Learn It HTML Template</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular.min.js"></script>
</head>
<body ng-app="sampleApp">
<div ng-controller="sampleController">
Are you going for party tonight: <input type="checkbox" ng-checked="checked" ng-click="toggleSwitch()">  
<br/> <br/>
You should go, Complete this example and rest examples you can learn tomorrow :), So click on the check box above: 
<br/> <br/>
<input id="checkSlave"  type="radio" ng-checked="checked" ng-click="toggleSwitch()">Yeah :)

</div>
<script>
var app = angular.module("sampleApp", []);
app.controller('sampleController', ['$scope', function ($scope) {
$scope.checked= false;

$scope.toggleSwitch=function(){
$scope.checked= !$scope.checked;
}
}]);
</script>
</body>
</html>
</html>

答案 3 :(得分:0)

这里没有指定双向绑定指令,即单选按钮的ng-model。

<input id="checkSlave" type="radio" ng-checked="checked" ng-model="checked">Yeah :)