この質問に似た状況があります。私のユニークなひねりは、ページが最初のレンダリング/バインドを完了した後のある時点で、(Web サービス呼び出しを介して) 非同期で「動的」情報を受け取ることです。したがって、私のディレクティブの流れは次のようになります。
** 更新: 完全な再現はこちら: http://plnkr.co/edit/TIVbhGFMAQpUaMxKp9Dc?p=info
Markup: <input dynValidate />
Directive:
app.directive('dynValidate',function($compile,$parse) {
restrict: 'A', require: 'ngModel', replace: false, terminal: true,
link: function (scope, elem, attr, ctrl) {
var validatorKey = elem.attr('ng-model') + 'Validation';
var installValidators = function () {
// ...add some ng-validation attributes
// (omitted for brevity, but does something like elem.attr('ng-minlength','5')
// based on metadata in the bound model
elem.removeAttr('dynValidate'); // Remove my own directive
$compile(elem)(scope); // recompile to incorporate new ng directives
}
};
// watch for the validation metadata to arrive asynchronously
scope.$watch(validatorKey, function() {
installValidators(attr, ctrl);
});
}
});
このような動作: 私のディレクティブ属性はいくつかの角度検証ディレクティブに置き換えられ、検証はこの要素のレベルで適切に機能しています。問題は、非同期再コンパイルにより、親の ng-form が要素を追跡できなくなったように見えることです。具体的には、入力が変更されたときに form.fieldName.$isDirty が true に設定されることはありません。元の要素を本質的に削除して別の要素に置き換えたので、それは理にかなっていますが、これが発生したことを親フォームに伝えることができるメカニズムがあり、それを接続する必要があるかどうか疑問に思っていますフォーム レベルでの $error/$dirty メカニズム。
ありがとう!