Why does the link evaluate the expression inside the directive using the link attribute, but the template does not? Note that I'm only using 'link' here for console.log illustrative purposes.
My end goal is to have data passed into a directive via its attribute and have that data render as its template.
index.html
<!DOCTYPE html>
<html>
<head>
<script data-require="angular.js@*" data-semver="1.3.7" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.7/angular.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="app.js"></script>
</head>
<body ng-app="myApp">
<div ng-controller="myCtrl">
<my-directive text="{{data}}" />
</div>
</body>
</html>
app.js
angular.module("myApp", []).directive("myDirective", function () {
return {
restrict: "E",
scope: {
text: "@text",
},
link: function(scope, element, attrs){
console.log(attrs.text);
},
template: function(element, attrs){
console.log(attrs.text);
}
};
}).controller('myCtrl',function($scope){
$scope.data = 'test';
});
output:
{{data}}
test
textattribute to specify what the contents of the directive are?