简体   繁体   English

如果选中,则禁用 angular 中的复选框?

[英]disable checkboxes in angular if checked?

I have an array of objects represented as checkboxes, I want to disable the checkbox if its checked (it's value is set from the database)我有一个表示为复选框的对象数组,如果选中了复选框,我想禁用它(它的值是从数据库中设置的)

<md-input-container ng-repeat="tab in allTabs">
                            <md-checkbox checklist-model="pop.tabs" checklist-value="tab" ng-disabled="disableCheckbox(this)">
                                {{tab.name}}
                            </md-checkbox>
                        </md-input-container>

in controller:在控制器中:

$scope.disableCheckbox = function (chk) {
        if (chk.isChecked)
            return true;
        else
            return false;
    }

but this disable all checkboxes!但这会禁用所有复选框!

EDIT编辑

I should use chk.checked instead, it works, but the problem is that once a checkbox is checked it's disabled, can I change this behavior?我应该改用chk.checked ,它可以工作,但问题是一旦选中复选框,它就会被禁用,我可以更改此行为吗?

The reason that happens is that you are using a function for ng-disabled.发生这种情况的原因是您正在使用 ng-disabled 的函数。 Functions are checked in every digest cycle.在每个摘要循环中检查函数。

With that said, my advice is that in your tab object create a property "isChecked" and set it on ng-click and the ng-disable should stay something like: ng-disabled = "!tab.isChecked".话虽如此,我的建议是在您的选项卡对象中创建一个属性“isChecked”并将其设置在 ng-click 上,并且 ng-disable 应保持如下所示:ng-disabled = "!tab.isChecked"。

Regards,问候,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM