javascript 始终启用 ng-disabled 的按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16527935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
button with ng-disabled always enabled
提问by Hilo
I'm trying to conditionally enable/disable my Save button using ng-disabled:
我正在尝试使用 ng-disabled 有条件地启用/禁用我的保存按钮:
<button type="button" title="Save Changes" ng-click="onSaveChanges()"
ng-disabled="{{!data.modified}}">
Save
</button>
I have a $scope.data.modified variable that changes to true when my data has been modified. Regardless whether that is true or false, the Save button is enabled. Element inspection reveals that the value of ng-disabled toggles between "true" and "false" as expect but the button is always enabled.
我有一个 $scope.data.modified 变量,当我的数据被修改时,它会变为 true。无论这是对还是错,都启用了“保存”按钮。元素检查显示 ng-disabled 的值按预期在“true”和“false”之间切换,但按钮始终处于启用状态。
回答by Rajkamal Subramanian
when you are using a angular js attribute (like ng-show, ng-hide, ng-disabled) it should be without the snake notation Ex.ng-disabled="!data.modified"
. For other ordinary attribute like class, id you have to use it with the snake notation. Ex. class={{aVaribaleinControllerScope}}
当您使用 angular js 属性(如 ng-show、ng-hide、ng-disabled)时,它应该没有蛇符号 Ex。ng-disabled="!data.modified"
. 对于其他普通属性,如 class、id,您必须将其与蛇形符号一起使用。前任。class={{aVaribaleinControllerScope}}
回答by georgeawg
Element inspection reveals that the value of ng-disabled toggles between "true" and "false" as expect but the button is always enabled.
元素检查显示 ng-disabled 的值按预期在“true”和“false”之间切换,但按钮始终处于启用状态。
Remove the double curly brackets ({{ }}
) from the Angular expression:
{{ }}
从 Angular 表达式中删除双大括号 ( ):
<button type="button" title="Save Changes" ng-click="onSaveChanges()"
?n?g?-?d?i?s?a?b?l?e?d?=?"?{?{?!?d?a?t?a?.?m?o?d?i?f?i?e?d?}?}?"?>?
ng-disabled="!data.modified">
Save
</button>
The double curly brackets convert the Angular expression to a string. In JavaScript, the string "false"
is truthy. Hence both strings "true"
and "false"
evaluate as truthy and the button is always enabled.
双大括号将 Angular 表达式转换为字符串。在 JavaScript 中,字符串"false"
是truthy。因此,字符串"true"
和"false"
评估都为真,并且按钮始终处于启用状态。
Directives which expect boolean values won't work.
期望布尔值的指令将不起作用。
See Why mixing interpolation and expressions is bad practice.