1

私はAngularが初めてで、ディレクティブを試しています。

使えるようになりたい

<div ng-messages="form.$error">
    <div ng-message="required">You did not enter a field</div>
</div>

そのようなカスタムディレクティブ内で、

<div class="form-group">
    <label class="form-label" for="s-input-{{name}}">
        {{text.human}}
        <span ng-if="text.subtext" class="form-hint">{{text.subtext}}</span>
    </label>

    <select ng-required="required" ng-model="model" class="form-control"  id="s-input-{{name}}" name="{{name}}" ng-options="t for t in text.select"></select>
    <div ng-messages="form.$error">
        <div ng-message="required">You did not enter a field</div>
    </div>
</div>

ディレクティブ要素の下に ng-messages を使用して渡すことで動作させることができますが、その中にはありません。

フォームをテンプレートに渡す方法が本当にわかりませんが、ng-messages からアクセスできるようになります。

必要最小限のセットアップを示すためにプランカーをセットアップしました。

http://plnkr.co/edit/ELx6Fd2eLN4CJjEnzRGM?p=preview

require: '^form',テンプレートの URL を使用/アクセスする方法がわかりませんが、フォームを要求する必要があることはわかっています。

私はしばらく頭をぶつけていたので、本当に助けていただければ幸いです!ありがとう。

4

2 に答える 2

1

いつでもフォームをディレクティブに渡すことができます。

JavaScript

app.directive('selectGov', function (){ 
   return {
        restrict: 'E',
        templateUrl: 'select.html',
        scope: {
            text: "=?",
            model: "=",
            required: "=",
            selectItems: "=?",
            name: "@?",
            form: "="
        },
    };
});

html

<select-gov model="person.phoneNumberType" text="q.phoneType" required="true" name="phoneType" form="myForm"></select-gov>
于 2016-04-15T20:45:08.657 に答える