2 つのテンプレートを要素に挿入して操作しようとしています。
<div
ic-first="foo"
ic-second="bar"
ic-third="baz"
ic-fourth="qux"
>
</div>
icFirstは、テンプレートを介して空の div をその要素の子として挿入する必要があります。icSecondは、その要素の 2 番目の子として 2 番目の div (多数のコンテンツを含む) を挿入する必要があるため、結果の html は次のようになります。
<div
ic-first="foo" // priority: 100
ic-second="bar" // priority: 50
ic-third="baz" // priority: 0
ic-fourth="qux" // priority: 0
>
<div id="foo"></div>
<div> <!-- a bunch of stuff from the templateUrl --> </div>
</div>
icFirstとicSecondの両方が、新しく作成されたコンテナーに他の要素を挿入します。
両方のディレクティブでディレクティブ テンプレート プロパティを指定すると、次のエラーが発生します。
エラー: 複数のディレクティブ [icFirst, icSecond] でテンプレートを要求しています:
<div ic-first
…</p>
transclude: true
両方のディレクティブに追加すると、 icFirstは問題なく実行されますが、同じ要素の他のディレクティブは実行されません。を設定するtransclude: 'element'
と、他のディレクティブは実行されますが、最初の子 ( $scope.firstObj
) が未定義であるというエラーが表示されます。
4 つのディレクティブはすべて、互いのスコープにアクセスする必要があるため、ほとんどの作業はそれらのコントローラーで行っています。
app.directive('icFirst', ['ic.config', function (icConfig) {
return {
restrict: 'A',
priority: 100,
template: '<div id="{{firstId}}"></div>',
replace: false,
transclude: 'element',
controller: function icFirst($scope, $element, $attrs) {
// …
$scope.firstId = $scope.opts.fooId;
$scope.firstElm = $element.children()[0];
$scope.firstObj = {}; // this is used by the other 3 directives
},
link: function(scope, elm, attrs) { … } // <- event binding
}
);
app.directive('icSecond', ['ic.config', function (icConfig) {
return {
restrict: 'A',
priority: 0,
templateUrl: 'views/foo.html',
replace: false,
transclude: 'element',
controller: function icSecond($scope, $element, $attrs) {
// …
$scope.secondElm = $element.children()[1];
$scope.secondObj = new Bar( $scope.firstObj );
// ^ is used by the remaining 2 directives & requires obj from icFirst
},
link: function(scope, elm, attrs) { … } // <- event binding
}
);
プル リクエスト#2433replace: false
で説明されているように、文書化された動作に一致するようにの動作を修正したことに注意してください。
コントローラーでインスタンス化$scope.firstObj
して、linkFn に設定しようとしましたが (linkFn が実行されるまでにトランスクルージョンが完了していることを願っています)、同じ問題が発生します。first-child は実際にはコメントのようです。