javascript AngularJS - ng-disabled true/false 不能正常工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30781744/
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
AngularJS - ng-disabled true/false not working properly
提问by Joseph Ocasio
I am trying to disable or enable a button when the value of a variable changes. ng-disabled is viewing the change of the value but is not disabling the button if it is enabled.
当变量的值发生变化时,我试图禁用或启用按钮。ng-disabled 正在查看值的更改,但如果启用,则不会禁用按钮。
Html:
网址:
<form name="myForm">
<input ng-pattern="onlyNumbers" ng-model="value" name="number" />
Valid? {{myForm.number.$valid}}
<button ng-disabled="{{!myForm.number.$valid}}"> Hello </button>
</form>
Js:
JS:
$scope.onlyNumbers = /^\d+$/;
回答by Zee
You don't have to use interpolation({{}}
) inside ng-disabled
:
您不必{{}}
在内部使用插值()ng-disabled
:
<button ng-disabled="!myForm.number.$valid"> Hello </button>
回答by Donal
Remove the brackets within ng-disabled:
删除 ng-disabled 中的括号:
<button ng-disabled="!myForm.number.$valid"> Hello </button>
回答by borracciaBlu
Try this:
试试这个:
<form name="myForm">
<input ng-pattern="onlyNumbers" ng-model="value" name="number" />
Valid? {{myForm.number.$valid}}
<button ng-disabled="myForm.number.$invalid"> Hello </button>
</form>