angular ng类将类添加到除当前类之外的所有类
angular ng-class add class to all except current
我在表中有一个复选框,当我选中一个复选时,我想将禁用的类添加到所有其他复选框中,我试图使用ng类实现这一点
<td scope="row">
<input
type="checkbox"
id="{{$index}}"
ng-class="{disabled: $index != currentid}"
ng-click="showOptions($event,'$index')" />
</td>
内部控制器
$scope.showOptions=function($event,id){
if($event.target.checked){
$scope.btns = true;
$scope.details_tab = true;
$scope.currentid = id;
}
else {
$scope.btns = false;
$scope.details_tab = false;
}
};
但是在页面加载所有复选框时使用此选项会禁用类。
调用showOptions
时,$index
不需要是值为'$index'
的字符串。它需要是值$index
:
<td scope="row">
<input type="checkbox"
id="{{$index}}"
ng-class="{disabled: $index != currentid}"
ng-click="showOptions($event, $index)"/>
</td>
否则,您将比较文字值$index
和整数索引(例如0
)。
但是在页面加载所有复选框时使用此选项会禁用类。
这是因为在页面加载时,所有$index值都不同于currentid,而currentid是您只在单击后设置的。
你需要确保你有一个复选框:
$scope.showOptions = function ($event, id) {
if ($event.target.checked) {
$scope.btns = true;
$scope.details_tab = true;
$scope.currentid = id;
$scope.disableCheckboxes = true; //checking a checkbox
} else {
$scope.btns = false;
$scope.details_tab = false;
$scope.disableCheckboxes = false; //unchecking a checked checkbox
}
};
在HTML中,您需要在ng-click
中考虑这一点(也不需要像其他人所说的$index
上的引号):
<td scope="row">
<input
type="checkbox"
id="{{$index}}"
ng-class="{disabled: disableCheckboxes && $index != currentid}"
ng-click="showOptions($event,$index)" />
</td>
也许您可以在currentid上添加一个验证。只有当"currentid"中有内容并且$index不是currentid时,才添加该类。现在,您可以在页面加载时将currentid设置为undefined。
<td scope="row"><input type="checkbox" id="{{$index}}" ng-class="{disabled: $index != currentid && currentid}" ng-click="showOptions($event,'$index')"></td>
相关文章:
- 正在ng重复元素上添加事件
- 通过jQuery添加ng样式属性,angular不更新
- 有没有添加一个ng点击到md背景
- 在控制器中添加$location依赖项时,ng-click停止激发
- angular元素在通过ng类添加类时不起作用
- ng重复向一个元素添加条件类
- 如何在多变量比较的基础上添加ng类
- 添加ng单击到highcharts ng柱状图xAxis标签
- 动态添加 ng 表单和验证
- 如何将静态字符串添加到受信任的 ng-src
- AngularJS:为ng-repeat中的图像添加切换功能
- 在所选元素的父元素上添加“ng-class”
- Ng-Repeat在末尾添加新项目
- 角度JS,将过滤条件添加到ng重复
- Angular ng sortable-如果我在一个栏中添加了一个搜索框,则拖放在两列之间无法正常工作
- 如何在数组中以ng个重复间隔应用新添加的对象
- angular ng类将类添加到除当前类之外的所有类
- ng只重复添加项目一次
- 动态添加ng模式,缺少验证类
- 将默认选项添加到ng-if