2016-04-26 9 views
4

すべてのフィールドが正しく入力されているときに有効にする必要がある送信確認ボタンがあります。バリデーションが複雑すぎるので、formName。$のみを使用して解決できないので、バリデーションに対応する関数を書く必要があります。私はshouldEnable関数が内部のモデル変更のそれぞれのためにトリガーになることを期待しています。しかし、それはしません。 Osにはこれに代わる方法がありますか?変更時にトリガする機能を持つng-disabled

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

答えて

4

あなたの関数はブール値を返す必要が期待

初期

<button ng-disabled="formName.$invalid">Submit</button> 

<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); 
}); 
0

あなたが角度のデータバインディングを使用している場合、あなたはこのようなものを使用する必要があります。

<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; 
     } 
    }) 
} 
関連する問題