gpt4 book ai didi

javascript - 如何让AngularJS默认选中一个复选框

转载 作者:行者123 更新时间:2023-11-28 18:43:42 24 4
gpt4 key购买 nike

当页面在两个切换复选框之间加载时,如何默认选中一个复选框。我的代码:

jsfiddle:http://jsfiddle.net/Lvc0u55v/456/

function MyCtrl($scope) {
$scope.changeAxis1 = function() {
if ($scope.thirtyDay) {
$scope.wholeTimeline = false;

}
};

$scope.changeAxis2 = function() {
if($scope.wholeTimeline) {
$scope.thirtyDay = false;

}
};
};

查看

 <label class='checkboxes' id='checkbox_1'>
<input type="checkbox" ng-model="thirtyDay" ng-change="changeAxis1()">
Two
</label>
<label class='checkboxes' id='checkbox_2'>
<input type="checkbox" ng-model="wholeTimeline" ng-change="changeAxis2()">
One
</label>

最佳答案

在 Controller 中将其中一个值初始化为 true:

function MyCtrl($scope) {
$scope.changeAxis1 = function() {
if ($scope.thirtyDay) {
$scope.wholeTimeline = false;
}
};

$scope.changeAxis2 = function() {
if($scope.wholeTimeline) {
$scope.thirtyDay = false;
}
};

$scope.thirtyDay = true;
};

您还需要在 View 中包含 ng-appng-controller 以便 Angular 执行任何操作:

<div class="checkbox" ng-app="" ng-controller="MyCtrl">

http://jsfiddle.net/Lvc0u55v/457/

关于javascript - 如何让AngularJS默认选中一个复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35720514/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com