次のディレクティブを想定します。
<validation-errors field="someField">Some errors: {{errors}}</validation-errors>
次のように簡単なディレクティブ関数を作成できると思いました。
return {
require: '^form',
restrict: 'E',
link: link,
scope: {
field: '@'
},
transclude: true,
template: '<span ng-show="errors" class="alert-danger" ng-transclude></span>'
};
function link(scope, el, attr, ctrl, transcludeFn) {
scope.errors = ctrl.Validator.getErrors(attr.field);
}
しかし、Transclusion is the process of extracting a collection of DOM element from one part of the DOM and copying them to another part of the DOM, while maintaining their connection to the original AngularJS scope from where they were taken.
(ドキュメントから)以来、スコープは思ったように機能しません。
だから私はこれを試しましたが、「いくつかのエラー」の部分が重複していることを除いて:
transcludeFn(function(clone, transScope) {
scope.errors = transScope.errors = ctrl.Validator.getErrors(attr.field);
el.append(clone);
});
を外すとうまくいきませんel.append(clone);
。
トランスクルージョンされたコンテンツがディレクティブ テンプレートのスコープを共有するようにする最善の方法は何ですか?