반응형
HTML을 각도 지시어에 전달하려면 어떻게 해야 합니까?
템플릿으로 각도 지시문을 작성하려고 하는데 div 안의 HTML도 잃고 싶지 않습니다.예를 들어 HTML로부터의 지시사항을 다음과 같이 호출합니다.
<div my-dir>
<div class="contents-i-want-to-keep"></div>
</div>
그리고 제 지시가 있습니다.
app.directive('myDir', [ '$compile', function($compile) {
return {
restrict: 'E',
link: function(scope, iElement, iAttrs){
// assigning things from iAttrs to scope goes here
},
scope: '@',
replace: false,
templateUrl: 'myDir.html'
};
}]);
다음으로 myDir.html이 있습니다.여기서 새로운 요소를 정의합니다.
<div class="example" style="background: blue; height: 30px; width: 30px"></div>
replace를 false로 설정해도 내부 contents-i-want-to-keep div가 손실됩니다.각도 문서에 대한 이해는 템플릿 뒤에 추가됩니다.(아마도 링크 기능을 통해) 결과를 유지할 수 있는 방법이 있습니까?
<div class="example" style="background: blue; height: 30px; width: 30px">
<div class="contents-i-want-to-keep"></div>
</div>
감사합니다!
를 사용해야 합니다.transclude: true
명령어 옵션에서 추가ng-transclude
템플릿에 추가:
<div class="example" style="…" ng-transclude></div>`
이 plunkr에는 원래 dom 요소를 유지하기 위해 템플릿에서 ng-transclude를 사용하는 방법의 예가 있습니다.
http://plnkr.co/edit/efyAiTmcKjnhRZMTlNGf
언급URL : https://stackoverflow.com/questions/16697721/how-to-pass-html-to-angular-directive
반응형
'programing' 카테고리의 다른 글
유형 'never'에 속성 'value'가 없습니다.mui에서 useRef 후크 사용 시 (0) | 2023.03.08 |
---|---|
Http Only cookie는 AJAX 요청에서 어떻게 작동합니까? (0) | 2023.03.08 |
데이터베이스에서 동적 HTML 문자열 컴파일 중 (0) | 2023.03.08 |
meta_query를 사용한 복잡한 custom_field 검색 (0) | 2023.03.08 |
requirejs를 사용하여 정적 JSON 파일을 로드하려면 어떻게 해야 합니까? (0) | 2023.03.08 |