在ng-repeat中过滤错误消息?

时间:2015-04-15 00:17:35

标签: javascript angularjs

我有一个多页(html模板)表单,我正在使用Angular进行验证。是否可以使用$ setValidity和ng-repeat使用字符串过滤器仅显示某些错误消息?如果没有,有什么建议吗?

我想捕获错误,$ setValidity,然后根据错误来自的表单页面有条件地显示某些错误消息。

以下是Angular验证码:

 ///TEAM DATA
    if (!$scope.formData.TeamContactFirstName) {
            $scope.signupform.$setValidity('TEAM: Please provide a Team Contact First Name', false)
            $scope.failedForms.team = true;
            isValid = false;
        }
        if (!$scope.formData.TeamContactLastName) {
            $scope.signupform.$setValidity('TEAM: Please provide a Team Contact Last Name', false)
            $scope.failedForms.team = true;
            isValid = false;
        }
//PROJECT BASICS
       if (!$scope.formData.Title) {
            $scope.signupform.$setValidity('BASICS: Please provide a project title.', false)
            $scope.failedForms.basics = true;
            isValid = false;
        }
if (!$scope.formData.ProjectLocation) {
        $scope.signupform.$setValidity('BASICS: Please provide the location of the project', false)
        $scope.failedForms.basics = true;
        isValid = false;
    }

这是转发器:

<div ng-show="signupform.$invalid" class="alert-box alert radius">
    <ul>
        <li ng-repeat="(key, errors) in signupform.$error">

            <ul>
                <li ng-repeat="e in errors"><strong>{{ key }}</strong>.</li>
            </ul>
        </li>
    </ul>
</div>

我希望只能使用此转发器显示TEAM DATA错误。过滤

1 个答案:

答案 0 :(得分:1)

很奇怪地使用$setValidity,但无论如何:

<div ng-show="signupform.$invalid" class="alert-box alert radius">
    <ul>
        <li ng-repeat="(key, errors) in signupform.$error">

            <ul>
                <li ng-repeat="e in errors" ng-if="key.indexOf('TEAM:') == 0"><strong>{{ key }}</strong>.</li>
            </ul>
        </li>
    </ul>
</div>