I have a submit validation button that need to be enable when all fields is being entered correctly. As the validation is too complex and is not able to be solve by using formName.$invalid alone, i need to write a function to cater the validation. I expect that the shouldEnable function to be trigger for each of the model changes inside. But it does not. Os is there any alternative for this?
我有一个提交验证按钮,需要在正确输入所有字段时启用。由于验证过于复杂,无法通过使用formName来解决。仅$ invalid,我需要编写一个函数来满足验证要求。我希望在内部的每个模型更改中触发shouldEnable函数。但事实并非如此。 Os还有其他选择吗?
Initial
初始
<button ng-disabled="formName.$invalid">Submit</button>
Expected
预期
<button ng-disabled="shouldEnable()">Submit</button>
$scope.shouldEnable = function() {
$scope.isEnable = true;
angular.forEach($scope.form.input2, function(val) {
if($scope.form.input2.inputA && $scope.form.input2.inputB) {
isEnable = false;
}
})
}
2 个解决方案
#1
4
Your function needs to return boolean value:
你的函数需要返回布尔值:
$scope.shouldEnable = function() {
var isEnable = true;
// make necessary checks
return isEnable;
}
#2
0
If you are using Angular's data-binding, You should use something like this :
如果您使用的是Angular的数据绑定,您应该使用以下内容:
<button ng-disabled="shouldEnable">Submit</button>
// yourObject is the object you've map in the form
$scope.$watch('yourObject', function(newObject) {
$scope.shouldEnable = isValid(newObject);
});
#1
4
Your function needs to return boolean value:
你的函数需要返回布尔值:
$scope.shouldEnable = function() {
var isEnable = true;
// make necessary checks
return isEnable;
}
#2
0
If you are using Angular's data-binding, You should use something like this :
如果您使用的是Angular的数据绑定,您应该使用以下内容:
<button ng-disabled="shouldEnable">Submit</button>
// yourObject is the object you've map in the form
$scope.$watch('yourObject', function(newObject) {
$scope.shouldEnable = isValid(newObject);
});