我想第一次将它插入带有隔离范围的指令中的标记中来访问已转换元素的范围.我可以使用transclude函数访问transcluded元素的克隆,但不是第一次插入元素.
我有以下HTML
<my-directive the-name="'John Doe'">
<my-div name="myDivName"></my-div>
</my-directive>
Run Code Online (Sandbox Code Playgroud)
我有两个指令.我想要转换my-directive的内容,并能够从为transcluded元素创建的作用域中访问名为"myDivName"的变量.该变量从"my-directive"指令的隔离范围中的变量"theName"获取其内容.
这是我的Javascript代码:
var app = angular.module('test', []);
app.directive('myDirective', function(){
return {
restrict: 'E',
template: '',
transclude: true,
scope:{
theName: '='
},
template: '<div>Hello I am My Directive and this content goes BEFORE the transclude<br><ng-transclude></ng-transclude><p>This element goes after the transclude</p></div>',
link: function(scope, element, attrs, ctrl, transclude){
transclude(function (clone, transcludeScope) {
transcludeScope.myDivName = scope.theName;
element.append(clone);//This line shouldn't be here. I just put it to illustrate that this clone has the right value in the …Run Code Online (Sandbox Code Playgroud)