Javascript ng已禁用,具有更改时触发的功能

Javascript ng已禁用,具有更改时触发的功能,javascript,angularjs,Javascript,Angularjs,我有一个提交验证按钮,需要在正确输入所有字段时启用该按钮。由于验证太复杂,无法单独使用formName解决。$invalid,我需要编写一个函数来满足验证的需要。我希望shouldEnable函数能够触发内部的每个模型更改。但事实并非如此。操作系统是否有其他选择 首字母 <button ng-disabled="formName.$invalid">Submit</button> 提交 期望 <button ng-disabled="shouldEnable()

我有一个提交验证按钮,需要在正确输入所有字段时启用该按钮。由于验证太复杂,无法单独使用formName解决。$invalid,我需要编写一个函数来满足验证的需要。我希望shouldEnable函数能够触发内部的每个模型更改。但事实并非如此。操作系统是否有其他选择

首字母

<button ng-disabled="formName.$invalid">Submit</button>
提交
期望

<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;
        }
    })
}
提交
$scope.shouldEnable=函数(){
$scope.isEnable=true;
angular.forEach($scope.form.input2,函数(val){
if($scope.form.input2.inputA&&$scope.form.input2.inputB){
isEnable=错误;
}
})
}

您的函数需要返回布尔值:

$scope.shouldEnable = function() {
    var isEnable = true;
    // make necessary checks
    return isEnable;
}

如果使用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);
});
提交
//yourObject是您在表单中映射的对象
$scope.$watch('yourObject',函数(newObject){
$scope.shouldEnable=isValid(newObject);
});