Javascript 角度-范围更改后禁用ng不工作
我有一个输入和一个按钮。我希望在输入为空或长度超过15个字符时禁用该按钮 HTMLJavascript 角度-范围更改后禁用ng不工作,javascript,angularjs,Javascript,Angularjs,我有一个输入和一个按钮。我希望在输入为空或长度超过15个字符时禁用该按钮 HTML <input type="text" id="addCustomer" ng-model="customerNumber"> <button id="addSingleCustomerButton" ng-disabled="!isValidSingleInput(customerNumber)" ng-click="addCustomer()"></button> 在add
<input type="text" id="addCustomer" ng-model="customerNumber">
<button id="addSingleCustomerButton" ng-disabled="!isValidSingleInput(customerNumber)" ng-click="addCustomer()"></button>
在addCustomer
函数的末尾,通过将作用域设置为$scope.customerNumber=''来清除输入字段代码>
但是,清除输入后,按钮不会再次禁用。我在isValidInput
函数中添加了一个日志,其中包含它的结果,在清除输入后,它返回false
。因此调用该函数并返回预期值,但不知何故,它对按钮的ng disabled
没有影响
编辑:添加功能addCustomer
EDIT2:认为这无关紧要,但问题是由Bootstrap的button.js引起的。如果我移除.button('loading')
和.button('reset')
它正在工作。在我看来,这无论如何都应该有效,因为我正在重置按钮后的范围。你能检查一下为什么它可能不适用于你吗
var-app=angular.module('myApp',[]);
应用程序控制器('myCtrl',函数($scope){
$scope.firstName=“John”;
$scope.lastName=“Doe”;
$scope.isValidSingleInput=函数(输入){
return(input!=undefined&&input.length>0&&input.length显示更多代码…因为我复制了您的代码并在这里尝试了它的工作状态很好!编辑了我的问题您可以尝试此-->?是的,我当然可以实现.button()
手动功能,但我认为它应该按原样工作……我的情况不同之处在于,我有一个异步调用来将客户号码保存在数据库中。然后在回调中清除输入。
$scope.isValidSingleInput = function(input) {
return (input != undefined && input.length > 0 && input.length <= 15);
}
$scope.addCustomer = function() {
$('#addSingleCustomerButton').button('loading');
// Call service to add customer number to database
CustomerService.add({}, payload, function(data) {
$('#addSingleCustomerButton').button('reset');
$scope.customerNumber = '';
});
}