0

カスタム検証のディレクティブを持ち、ng-messages を使用してそれらを表示するモジュールを構築しようとしています

検証は正規表現を介して行われます。

私が見ているエラーは次のとおりです。

エラー: [$compile:ctreq] ディレクティブ 'ngMessage' で必要なコントローラー 'ngMessages' が見つかりません!

私のコードは次のようになります。

検証ディレクティブ:

module LoginModule {

    'use strict';

    /***** REGEX *****/
    export class regExp {
        public ID_OR_PASSPORT = /^[0-9]{9}$/;
        public USERNAME_SINGLE_WORD = /^[A-Za-z0-9à-ú-_\.]{6,8}$/;
        public PASSWORD = /^[A-Za-z0-9à-ú-_\.]{8}$/;
        public EMAIL = /^([\?!\/]*)+\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
        public ALPHANUMERIC = /^[a-zA-Z0-9]+$/;
        public NUM = /^[0-9]{1,}[.]{0,1}[0-9]{0,}$/;
        public PHONE_BODY = /^[0-9]{7}$/;
    }

    angular.module("LoginModule").value('REG_EXP', LoginModule.regExp);
}

module LoginModule {

    export class uniIdValidator implements ng.IDirective {
        constructor(public REG_EXP) { }
        public restrict: string = 'A';
        public require: string[] = ['ngModel'];
        public templateUrl: string = 'errorMessages.html'; //this should be external file, will be used later
        public replace: boolean = false;
        public link: Function = (scope: ng.IScope,
            element: ng.IAugmentedJQuery,
            attrs: ng.IAttributes,
            ctrls: any) => {
            ctrls[0].$validators.userId = function (modelValue) {
                //return REG_EXP.ID_OR_PASSPORT.test(modelValue);
                console.log(modelValue)
                return modelValue;
            };
        }
    }

    angular.module('LoginModule')
        .directive('uniIdValidator', ['REG_EXP',
            (REG_EXP) => { return new LoginModule.uniIdValidator(REG_EXP) }]);
}

私のhtmlで:

<Input ... uni-id-validator />
<div class="login-form__error-box" ng-messages="loginForm.loginFormId.$error">
                <span class="login-form__error-msg" ng-message="userId">error in ID</span>
                <span ng-message="required">This is required</span>
 </div>

私のアプリモジュール:

((): void=> {
    var appLogin = angular.module("LoginModule", ['ngRoute', 'ngMessages']);
    appLogin.config(LoginModule.Routes.configureRoutes);
})() 

私のコントローラーは長すぎてここに投稿できませんが、ngMessages の注入はありません (静的注入も使用してみました --> 機能しませんでした)

明らかに私は何か間違ったことをしていますが、何がわかりません。(これは私がここで以前に抱えていた問題の続きです)

4

2 に答える 2

1

ngMessages モジュールには個別の angular-messages.js ファイルが必要です。スクリプト ファイルをロードするメイン アプリの index.html ページが表示されませんが、必要なファイルが含まれていることを確認してください。そうでない場合は、このメッセージが表示されます。ngMessages ドキュメントで入手可能なダウンロードと CDN に関する情報

于 2015-09-06T18:34:05.740 に答える
0

最終的に、エラーはテンプレート ファイル (errorMessages.html) にありました。そこに私が持っていた:

<span ng-message="required">Required</span>

私のページには存在するがテンプレートには存在しないコントローラー「ng-messages」を探していたため、エラーが発生しました。ディレクティブからテンプレートの使用を削除し、html で ng-messages-include を使用しました。(バージョン> 1.4.0bからの変更に注意してください。別の要素にする必要があります)

于 2015-09-07T07:27:42.700 に答える