如何将单选按钮标记为选中Angular with ng click/ng change
How to mark a radio button as checked in Angular with ng-click / ng-change?
http://plnkr.co/edit/rXWUUjLtJi79z2hF1uVX?p=preview
我有一个非常简单的plnkr,基本上当单击li
元素时,我希望选中单选按钮。
以下是如何实现的?现在没有收音机被选中。
如果我删除了ng点击,那么用户可以只点击单选按钮,但如果用户点击li
内部的文本/标签,我也希望选择单选按钮,这就是为什么我试图让li
上的ng-click
以这种方式工作。
angular.module('app', [])
.controller('mainController', ['$scope', '$rootScope',
function($scope,$rootScope) {
$scope.toggleTags = function(type) {
console.log(type);
switch(type) {
case 'watchlist':
$scope.watchlist = true;
$scope.private = false;
break;
case 'private':
$scope.watchlist = false;
$scope.private = true;
break;
}
};
}]);
body { font-family: Arial, sans-serif; }
li {
margin: 10px;
list-style: none;
width: 100%;
clear: both;
cursor: pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="app" ng-controller="mainController">
<ul>
<li ng-click="toggleTags('watchlist')">
<input type="radio"
name="personalGroup"
ng-model="watchlist"
ng-change="toggleTags('watchlist')"
value="watchlist">
My Watchlist
</li>
<li ng-click="toggleTags('private')">
<input type="radio"
name="personalGroup"
ng-model="private"
ng-change="toggleTags('private')"
value="private">
My Private Tags
</li>
</ul>
</body>
对于这个示例,我不会使用ng-click
来执行此操作。相反,我会使用HTMLDOM对象<label for="radio_button_id_here">
。
HTML
<li>
<input type="radio" name="personalGroup" ng-model="watchlist" value="watchlist" id="watchlist">
<label for="watchlist">My Watchlist</label>
</li>
<li>
<input type="radio" name="personalGroup" ng-model="private" value="private" id="private">
<label for="private">My Private Tags</label>
</li>
相关文章:
- $localStorage array select with ng选项工作不正常
- Angularjs slider with ng-change
- AngularJS select with ng选项不更新父作用域中的引用对象属性
- Angularjs ng-show not working with checkbox with default val
- Delete with ng-if?
- JavaScript array.push (object) with Angular ng-repeat 无法按预期运
- ng-options from a json with angularjs
- Angularjs with dynamic model in ng-repeat
- Angular JS ng-switch with ng-include?
- Problems with ng-include
- AngularJS Function with ng-if inside ng-repeat
- angularjs ng-options with formate date delete duplicates
- angularjs $index when ng-hide with ng-repeat
- Angular js ng-show and ng-hide with animation using toggle c
- Angulajs ng-options from a json with child object
- Angularjs ng-repeat with conditions
- Susy gallery with AngularJS (ng-repeat) - 不填补 ng-hide 的空白
- Nested ng-repeat with AngularJS
- Problems with ng-repeat, $scope, $http
- angularjs ng-show with promise expression