我的按钮可以选中ng-grid中的所有复选框,效果很好。但是,我也想实现使用同一按钮取消选择。
这是原始代码
app.directive('selectAll',function(){
return{
restrict: 'A',
link: function(scope,element,attr){
element.click(function() {
$('.ngViewport.ng-scope input[type="checkbox"]').prop('checked', true);
});
}
};
});
我做了一些谷歌搜索并基于其他示例后的尝试:
app.directive('selectAll',function(){
return{
restrict: 'A',
link: function(scope,element,attr){
element.click(function() {
if (this.checked) {
$('.ngViewport.ng-scope input[type="checkbox"]').prop('checked', true);
}
else{
$('.ngViewport.ng-scope input[type="checkbox"]').prop('checked', false);
}
});
}
};
});
由于某种原因,Angular不喜欢this.checked?不知道从这里去哪里。
朋克
您正在检查按钮的Checked属性。
button元素没有这样的属性。
我将向按钮元素添加一个类。并基于此设置复选框的checked属性。这是使用jQuery。
但是使用Angular应该避免使用jQuery作为最后的手段。相反,应该使用模型来保留当前状态的值。
element.click(function() {
var $this = $(this),
isChecked = false;
if ($this.hasClass('checked')) {
$this.removeClass('checked');
} else {
$this.addClass('checked');
isChecked = true;
}
$('.ngViewport.ng-scope input[type="checkbox"]').prop('checked', isChecked);
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句