2016-11-18 73 views
0

我試過了,如果我選中複選框,那麼單選按鈕也會被選中。當我檢查複選框時它會很好。如何在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> 

任何人都可以請幫忙嗎?

回答

1

試試這個

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

 

 
}]); \t \t 
 
</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>

+0

它不工作..現在,如果我取消選中複選框,單選按鈕保持選中狀態。 @priya – Idris

+0

只需檢查即時。我剛剛更新了它。 –

+0

這是這種情況下更好的方法!應該被接受回答 –

0

在這裏你沒有指定雙向綁定指令即NG-模型單選按鈕。

<input id="checkSlave" type="radio" ng-checked="checked" ng-model="checked">Yeah :) 
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事件。

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

它的工作..我想檢查ng-model – Idris