gpt4 book ai didi

javascript - 将 Ctrl 中的 switch 语句减少为禁用输入字段(AngularJS)

转载 作者:行者123 更新时间:2023-11-28 15:18:50 25 4
gpt4 key购买 nike

通常,代码语句可以工作,但我想这不是一个好的做法,因为变量在 switch 语句的每种情况下都会重复。所以我不知道如何减少代码或者是否有替代方案可以切换?

首先是Ctrl中的代码:

$scope.searchDisabled = function (num) {
switch (num) {
case 1:
$scope.disLname = false;
$scope.disFname = true;
$scope.disGroup = true;
$scope.group = {};
break;
case 2:
$scope.disLname = true;
$scope.disFname = false;
$scope.disGroup = true;
$scope.group = {};
break;
case 3:
$scope.disLname = true;
$scope.disFname = true;
$scope.disGroup = false;
break;
case 4:
$scope.disLname = true;
$scope.disFname = true;
$scope.disGroup = true;
$scope.group = {};
break;
default:
$log.error('ERR! variable isn\'t a num:', num);
break;
}
}
};

最后我的观点:

<form class="form-horizontal" name="searchForm">
<div class="form-group form-group-sm">
<label class="col-sm-3 control-label">Lastname:</label>
<div class="col-sm-8">
<input type="text" class="form-control" placeholder="Search lastname" name="Lname" ng-model="search.lname" ng-click="searchDisabled(1)" ng-disabled="disLname" />
</div>
<div class="col-sm-1">
<input type="radio" class="radio" name="checked" ng-click="searchDisabled(1)" ng-model="formRadio.checked" value="1" ng-hide="!disLname" />
</div><!--end radio btn -->
</div>
<div class="form-group form-group-sm">
<label class="col-sm-3 control-label">Firstname:</label>
<div class="col-sm-8">
<input type="text" class="form-control" placeholder="Search firstname" name="Fname" ng-model="search.fname" ng-click="searchDisabled(2)" ng-disabled="disFname" />
</div>
<div class="col-sm-1">
<input type="radio" class="radio" name="checked" ng-click="searchDisabled(2)" ng-model="formRadio.checked" value="2" ng-hide="!disFname" />
</div><!--end radio btn -->
</div>
<div class="form-group form-group-sm">
<label class="col-sm-3 control-label">Group:</label>
<div class="col-sm-8">
<ui-select ng-model="group.selected" theme="selectize" ng-click="searchDisabled(3)" ng-disabled="disGroup">
<ui-select-match placeholder="Choose a group">
{{$select.selected.name}}
</ui-select-match>
<ui-select-choices repeat="group in groups | filter: $select.search">
<span ng-bind-html="group.name | highlight: $select.search"></span>
<small ng-bind-html="group.code | highlight: $select.search"></small>
</ui-select-choices>
</ui-select>
</div><!--end select group -->
<div class="col-sm-1">
<input type="radio" class="radio" name="checked" ng-click="searchDisabled(3)" ng-model="formRadio.checked" value="3" ng-hide="!disGroup" />
</div><!--end radio btn -->
</div>

对于“组”字段,我使用 angularjs 的 ui-select 模块。但是我的问题是如何减少或替换 switch 语句?

最佳答案

规则非常简单:

  • 除非值为 1,否则 disLname 为 true
  • 除非值为 2,否则 disFname 为 true
  • 除非值为 3,否则 disGroup 为 true
  • 组为 {},除非值为 3

我还添加了对 num 不是数字以及 num 越界的检查。

Demo - 查看左下 Pane 的控制台。

scope.searchDisabled = function (num) {
var value = parseInt(num, 10);

if(isNaN(num)) {
$log.error('ERR! variable isn\'t a num:', num);
return;
}

if(num < 1 || num > 4) {
$log.error('ERR! variable is out of range:', num);
return;
}

$scope.disLname = value !== 1;
$scope.disFname = value !== 2;
$scope.disGroup = value !== 3;
$scope.group = value === 3 ? undefined : {};
};

关于javascript - 将 Ctrl 中的 switch 语句减少为禁用输入字段(AngularJS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32425490/

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