1

ui-validate ユーティリティを使用してい ます https://github.com/angular-ui/ui-validate

問題は、入力フィールドのないフォームで式を検証することです。例えば?私はオブジェクトを持っています

$scope.item = { field1 : 0, field2: 0, field3: 0 };

次の式を指定して、エラーを受け取りたいです。field1 + field2 + field3 == 0

フォーム全体の共通の検証です。一部の入力用ではありません。

4

2 に答える 2

1

次のような小さな関数を書くことができます (よくわかりませんが、ui-validateこれを使用する必要があります)。

$scope.validate = function () {
    var sum = 0;

    // Sum every non Angular key
    angular.forEach($scope.items, function (value, key) {
        // You can also add another check below "angular.isNumber(value)" if you have some text fields
        if (key.charAt(0) !== '$') {
            // Call "parseInt()" method here if values are in string
            sum += value;
        }
    });

    return sum !== 0;
}

次に、フォームのどこかに表示します。

<form>
    <div ng-show="!validate()">There is some error. Sum can't be zero</div>
    <!-- Your fields below -->
</form>
于 2015-12-08T07:53:32.880 に答える