Here’s a simple example how to create your own hyperlink directive, demonstrating changing the DOM with directives.
Usage:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.directive('hyperlink', function(){ | |
return { | |
transclude: true, | |
replace: true, | |
template: '<a ng-transclude></a>', | |
} | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.directive('hyperlink', function(){ | |
return { | |
restrict: 'E', | |
transclude: true, | |
link: function (scope, element, attrs, ctrl, transclude) { | |
transclude(scope, function (clone) { | |
var anchorTag = angular.element("<a href='" + attrs.ngHref + "'></a>"); | |
anchorTag.append(clone); | |
element.append(anchorTag); | |
}); | |
} | |
} | |
}) |
Here I’ve kept the hyperlink element in the DOM as this is considered a best practise by the Angular team since v1.3.
No comments:
Post a Comment