简体   繁体   English

如何在数据绑定中使用ng-disabled

[英]how to use ng-disabled with data binding

I am trying to disabled the button when the binding data (level) is 1. 当绑定数据(级别)为1时,我试图禁用该按钮。

ng-disabled="$ctrl.disabled($ctrl.selected.item.level)" I tried this but doesn't work ng-disabled =“ $ ctrl.disabled($ ctrl.selected.item.level)”我试过了,但是没用

index.html: index.html的:

<button type="button" class="btn btn-sm btn-outline-secondary" ng-click="$ctrl.delete()" ng-disabled="$ctrl.disabled($ctrl.selected.item.level)">Delete</button>

index.ts: index.ts:

disabled = (level) => {
        if (level === 1) {
            return false;
        } else {
            true;
        }
    }

you should return true to disable the button. 您应该返回true以禁用该按钮。 ng-disabled will disable the button when the called function will return true. ng-disabled将在被调用函数返回true时禁用按钮。 in your case you are returning false in case, level equals 1. Also, Make sure the data types match when you use === operator 在您的情况下,如果返回false,则级别等于1。此外,请确保在使用===运算符时数据类型匹配

disabled = (level) => {
    if (level === 1) {
        return true;//this should be true when the level is 1, you want to disable.
    } else {
        false;//this should be false, cause you want to enable.
    }
}

Your code 您的密码

disabled = (level) => {
        if (level === 1) {
            return false;
        } else {
            true;
        }
    }

It should be true to work because if the expression is true, then the disabled attribute will be set on the element so your code becomes, 它应该是正确的,因为如果表达式为true,则将在元素上设置disable属性,以便您的代码成为:

disabled = (level) => {
    if (level === 1) {
        return true;//change here true 
    } else {
        false;
    }
}

or 要么

(level === 1) ? return true : false;

see this document of angularjs 看到这份angularjs文件

I know there are a few answers out there, but I just wanted to show a simplified version: 我知道那里有一些答案,但是我只想显示一个简化版本:

ng-disabled="$ctrl.selected.item.level === 1"

Or if you need that disabled variable for anything else, just use this: 或者,如果您需要该disabled变量来做其他任何事情,只需使用以下命令:

disabled = (level) => { return level === 1;}

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

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