2016-07-01 2 views
2

앵귤러 1.5 도입 multi-slot transclusion.transclude 함수에서 중계 슬롯에 어떻게 액세스합니까?

transclude 개체 : the docs에 따르면 {slotA '? myCustomElement'는} 그렇지 않은

불행하게도 $의 transclude 기능을 통해 액세스 할 수있는 slotA 슬롯에 요소를 매핑 이것의 예를 든다.

$transclude(function(clone, scope) { 
    element.append(clone); 
    transcludedContent = clone; 
    transclusionScope = scope; 
}); 

누군가가 $의 transclude 기능을 사용하여 각 슬롯에 액세스하는 방법에 대한 도움이 되거 수 : 전혀 슬롯을 언급하지 않을 수 있습니다 유일한 예?

답변

11

비슷한 문제가 있었지만 ng-transclude 소스 코드를 읽는 것이 도움이됩니다. $ transclude 함수의 세 번째 인수가 슬롯 이름 인 것으로 판명되었습니다.

https://github.com/angular/angular.js/blob/master/src/ng/directive/ngTransclude.js#L190

간단한 예 :

angular.module('app', []); 
 

 
angular 
 
    .module('app') 
 
    .directive('dir', function() { 
 
    return { 
 
     transclude: { 
 
     a: 'aT', 
 
     b: 'bT' 
 
     }, 
 
     link: function (scope, elem, attrs, ctrl, transclude) { 
 
     transclude(function (content) { 
 
      elem.append('<div>a</div>'); 
 
      elem.append(content); 
 
     }, null, 'a'); 
 
     
 
     transclude(function (content) { 
 
      elem.append('<div>b</div>'); 
 
      elem.append(content); 
 
     }, null, 'b'); 
 
     } 
 
    }; 
 
    });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.js"></script> 
 

 
<div ng-app="app"> 
 
    <dir> 
 
    <a-t>content of a</a-t> 
 
    <b-t>content of b</b-t> 
 
    </dir> 
 
</div>

+1

당신이 null''에 전달하는 두 번째 매개 변수는 무엇입니까? – adamdport

+0

범위. 원하는 경우 transclude 범위를 사용자 정의 범위로 변경할 수 있습니다. – sielakos

+5

100 % 정확하지 않습니다. 범위는 첫 번째 옵션 (여기에서는 : 사용되지 않음) 매개 변수로 변경할 수 있습니다. 'null'은'futureParentElement' 매개 변수에 적용됩니다. 슬롯 매개 변수는 정확합니다. 완전한 선언은'function ([scope], cloneLinkingFn, futureParentElement, slotName)'입니다 (출처 : https://docs.angularjs.org/api/ng/service/$compile) –