Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit 24ac328

Browse files
devversionThomasBurleson
authored andcommitted
docs(anchor): only create anchors inside of md-content. (#9735)
* Currently anchors will be always created, regardless of being inside of a toolbar or inside of a sidenav. * The anchors should be only created inside of a `md-content` element.
1 parent 1b9245a commit 24ac328

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

docs/app/js/anchor.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,32 @@
66
.directive('h2', MdAnchorDirective)
77
.directive('h1', MdAnchorDirective);
88

9-
function MdAnchorDirective($mdUtil) {
9+
function MdAnchorDirective($mdUtil, $compile) {
1010

1111
/** @const @type {RegExp} */
1212
var unsafeCharRegex = /[&\s+$,:;=?@"#{}|^~[`%!'\].\/()*\\]/g;
1313

1414
return {
1515
restrict: 'E',
1616
scope: {},
17-
template: '<a class="docs-anchor" ng-href="#{{ name }}" name="{{ name }}" ng-transclude></a>',
18-
transclude: true,
17+
require: '^?mdContent',
1918
link: postLink
2019
};
2120

22-
function postLink(scope, element) {
21+
function postLink(scope, element, attr, ctrl) {
22+
23+
// Only create anchors when being inside of a md-content.
24+
if (!ctrl) {
25+
return;
26+
}
27+
28+
var anchorEl = $compile('<a class="docs-anchor" ng-href="#{{ name }}" name="{{ name }}"></a>')(scope);
29+
30+
// Wrap contents inside of the anchor element.
31+
anchorEl.append(element.contents());
32+
33+
// Append the anchor element to the directive element.
34+
element.append(anchorEl);
2335

2436
// Delay the URL creation, because the inner text might be not interpolated yet.
2537
$mdUtil.nextTick(createContentURL);
@@ -40,6 +52,6 @@
4052
}
4153

4254
// Manually specify $inject because Strict DI is enabled.
43-
MdAnchorDirective.$inject = ['$mdUtil'];
55+
MdAnchorDirective.$inject = ['$mdUtil', '$compile'];
4456

4557
})();

0 commit comments

Comments
 (0)