AngularJS:验证依赖字段

时间:2015-11-24 11:03:38

标签: angularjs validation directive

我创建了一个角度指令来验证必须总计达100%的百分比列表。问题是一个字段的有效性取决于另一个字段的有效性。我在我的指令中做的第一件事是尝试将表单有效性设置为有效以减轻这种情况 - 但这需要访问表单INgFormController并且我只能直接访问当前表单输入字段

link: function ($scope: angular.IScope, element: JQuery, attributes: ITimePercentageAttributes, ctrl: angular.INgModelController /*: IArticleRegistrationsController*/) {
    if (!ctrl) return;

    ctrl.$validators["percentage"] = function (modelValue, viewValue) {
        (<IArticleRegistrationsScope>$scope.$parent.$parent).timeRegistrationForm.$setValidity("percentage", true, ????????)

    //...calculate sum and return true/false...

$ setValidity方法缺少第三个参数。

相关的HTML模板代码如下:

<form name="timeRegistrationForm">
    <div class="panel panel-info">
        ...
        <table class="table panel-body">
            <thead>
                ...
            </thead>
            <tbody>
                <tr ng-repeat="timeAllocation in requisition.TimeAllocations">
                    <td class="adjust-padding-when-xs">
                        <input ng-model="timeAllocation.Percentage"
                                ng-model-options="{ updateOn: 'blur' }"
                                name="percentage{{$index}}"
                                ng-change="vm.percentageUsedChange(requisition)"
                                min="0"
                                max="100"
                                time-percentage />
                    </td>
                </tr>
                ...
            </tbody>
        </table>
        ...
    </div>
</form>

0 个答案:

没有答案