2014-07-07 65 views
1

定製指令第一子html我有一個angularjs指令,其中我打開一個模板:包括在AngularJS

<div class="step" id="stepId" > 
</div> 

這裏的指令聲明:

angular.module('PortfolioApp') 
    .directive('slideStep', function() { 
return { 
    templateUrl: appUrl + '/templates/slideStep.html', 
    restrict: 'E', 
     link: function postLink(scope, element, attrs) { 
     //element.text('this is the slideStep directive'); 
      console.log("element text" +element.text()); 
     }, 
    replace:true 
}; 
}); 

我所試圖實現的,是我使用該指令時包含的html包含在替換的html中。因此例如:

<slide-step> 
    <img src="images/HAC0.jpg"/> 
</slide-step> 

成爲

<div class="step" id="stepId" > 
    <img src="images/HAC0.jpg"/> 
</div> 

有沒有執行此至極的方式允許元素的HTML的第一個孩子的簡單結合自定義元素?

+0

我不明白你的問題是什麼。 「元素的HTML的第一個孩子到自定義元素的簡單綁定」是什麼意思? – tmuecksch

+0

我很抱歉不清楚。我的意思是「嵌入HTML文件中包含的指令被使用的HTML」。所以,如果我的指令有內部的一些HTML元素,如 一些文本 替換HTML變爲:

some text
<! - 這是模板HTML - > – suprandr

回答

3

您需要使用transclusion:

https://docs.angularjs.org/api/ng/directive/ngTransclude

angular.module('PortfolioApp') 
    .directive('slideStep', function() { 
return { 
    templateUrl: appUrl + '/templates/slideStep.html', 
    restrict: 'E', 
     link: function postLink(scope, element, attrs) { 
     //element.text('this is the slideStep directive'); 
      console.log("element text" +element.text()); 
     }, 
    transclude:true, 
}; 
}); 

,並在你的指令模板:

<div class="step" id="stepId" ng-transclude> 
</div> 
+0

感謝,這正是我在找什麼,儘管我注意到,儘管html是正確的,但使用由angular生成的html的其他框架不起作用(在這種情況下,impress.js),可能是因爲後者在角度依然不存在時搜索元素不編譯所有指令元素。 – suprandr