简体   繁体   English

输入文本为空时,Angular JS 禁用按钮

[英]Angular JS disabled button when input text empty

I start in angular JS and I want to disable (attribute "ng-disabled") a button while my input text "login" and "password" are empty.我从 angular JS 开始,我想禁用(属性“ng-disabled”)一个按钮,而我的输入文本“登录”和“密码”为空。 The problem is that, by default the text of input-text "login" is "LOGIN" and the text of input-text "password" is "PASSWORD", so my inputs are never empty...问题是,默认情况下输入文本“登录”的文本是“登录”,输入文本“密码”的文本是“密码”,所以我的输入永远不会为空......

Help,帮助,

Thanks谢谢

Simply use a placeholder instead of populating it with something只需使用占位符,而不是用某些东西填充它

 var app = angular.module('myApp', []); app.controller('myCtrl', function($scope) { $scope.pass = ""; // empty, instead of "PASSWORD" $scope.foo = () => { console.log($scope.pass); }; });
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script> <div ng-app="myApp" ng-controller="myCtrl"> <input placeholder="PASSWORD" ng-model="pass"> <button ng-click="foo()" ng-disabled="!pass">Sign in</button> </div>

Here is another example that suits your description, where you can populate it with some text, but still have the button disabled.这是另一个适合您的描述的示例,您可以在其中填充一些文本,但仍禁用按钮。 It uses ng-focus to reset the input field if it is selected, and it has $dirty checking, to only allow to press the button when the input has been altered/changed.如果它被选中,它使用ng-focus来重置输入字段,并且它有$dirty检查,只允许在输入被更改/更改时按下按钮。

 var app = angular.module('myApp', []); app.controller('myCtrl', function($scope) { $scope.pass = "PASSWORD"; $scope.foo = () => { console.log($scope.pass); }; });
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script> <div ng-app="myApp" ng-controller="myCtrl"> <form name="form"> <input ng-focus="pass=''" name="pass" ng-model="pass"> <button ng-click="foo()" ng-disabled="!form.pass.$dirty || !pass">Sign in</button> </form> </div>

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

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