련 지시어를 생성하는 내용의 테이블
-
21-12-2019 - |
문제
상상 html 텍스트를 포함하는 헤드라인(h1-h6).내 situtation 에 그것은 또한 현재스에서는 html 페이지입니다.그래서 사용 jQuery 나는 뭔가 $('.text').find('h1,h2,h3,h4,h5,h6')
을 추출하 모니다.
하지만 내가 사용하고 싶지 않 jQuery 또는 기타 무거운 framework.이렇게 하려면 어떻게 해야 합니까으로련?기억하시기 바랍 내가 필요로 하는 헤드라인을 올바른 순서를 표시로 사용할 수 있는 기능입니다.
해결책
여기서는 최종 해결책이 있습니다.NG-Model Part는 텍스트가 업데이트 될 때 헤드 라인을 업데이트하는 데 사용됩니다.
.directive('tableOfContents', function(){
return {
restrict:'A',
require:'?ngModel',
link : function(scope, elm, attrs,ngModel) {
function updateHeadlines() {
scope.headlines=[];
angular.forEach(elm[0].querySelectorAll('h1,h2,h3,h4,h5,h6'), function(e){
scope.headlines.push({
level: e.tagName[1],
label: angular.element(e).text(),
element: e
});
});
}
// avoid memoryleaks from dom references
scope.$on('$destroy',function(){
scope.headlines=[];
});
// scroll to one of the headlines
scope.scrollTo=function(headline){
headline.element.scrollIntoView();
}
// when the html updates whe update the headlines
ngModel.$render = updateHeadlines;
updateHeadlines();
}
}
})
.
사용법 :
<a ng-repeat="headline in headlines" ng-click="scrollTo(headline)">{{headline.label}}</a>
<div table-of-contents ng-model="html">{{html}}</div>
. 제휴하지 않습니다 StackOverflow