【发布时间】:2016-05-07 16:48:22
【问题描述】:
我希望能够获得嵌入的内容。将其应用于指令中的 ng-repeat。然后将每个 ng-repeat 的作用域应用到转入内容的克隆。
在另一个指令A的一部分中我有这个:
...
<directive-b>
// title is exposed here to directiveA but I want it to be changed to ng-repeat scope later
<p>Hello {{title}}</p>
</directive-b>
...
在directiveB指令内
(function() {
'use strict';
angular.module('app')
.directive('directiveB', DirectiveB);
function DirectiveB() {
return {
restrict: 'E',
transclude: true,
scope: {},
bindToController: {},
compile: DirectiveBCompile,
controller: DirectiveBController,
controllerAs: 'vm',
templateUrl: 'directive-b.partial.html'
}
}
function DirectiveBCompile(cElem, cAttr, cTransclude) {
// cTransclude is deprecated... so cant do this here
return InfiniteScrollLink;
}
function DirectiveBController() {
var vm = this;
}
function DirectiveBLink(scope, element, attrs, controller, transclude) {
// ideally would be great to somehow apply ng-repeat scope to each transcluded element here, but I couldn't
scope.data = [{
title: 12345
}, {
title: 345245
}, {
title: 32452345
}];
// this scope doesn't get picked up either
scope.title = "12345";
}
})();
内部指令B指令部分
<ul>
<li data-ng-repeat="data in vm.data" data-ng-transclude>
</li>
</ul>
有什么方法可以将 ng-repeat 中的“数据”作为 transclude 的范围传递?
我想看到的是:
<ul>
<li>
<p>Hello 12345</p>
</li>
<li>
<p>Hello 345245</p>
</li>
<li>
<p>Hello 32452345</p>
</li>
</ul>
【问题讨论】:
-
也许可以创建一个子 div,或者找出 $scope.data 的列出位置
-
可能是 data-ng-transclude="data"?我已经有一段时间没有使用 angular
-
data-ng-transclude="data" 用于多槽嵌入,例如
transclude: { * 'data': '?data',将指向包含在 transclude 中的数据元素 -
我也在使用 angular 1.4.7,在 1.5 中引入了多槽嵌入 :(
标签: javascript angularjs