[英]How to create new tag element in Angular.js
I want to create new element in Angular.js 我想在Angular.js中创建新元素
<link url="http://google.com">google</link>
that will be converted to <a>
tag, How can I do this? 将会转换为
<a>
标记,我该怎么做?
The code so far: 到目前为止的代码:
var app = angular.module('app', []);
app.directive('link', function() {
return {
restrict: 'E',
transclude: true,
replace: true,
scope: false,
template: '<a href="" ng-transclude></a>';
}
});
but It render google after a link and I don't know how to get the url. 但它会在链接后显示google,但我不知道该如何获取网址。
You can get it in the linking function. 您可以在链接功能中获得它。 Your directive would look like this:
您的指令如下所示:
app.directive('link', function() {
return {
restrict: 'E',
transclude: true,
replace: true,
scope: false,
template: '<a href="{{url}}" ng-transclude></a>',
link: function(scope, element, attrs) {
scope.url = attrs.url;
}
}
});
If you're ok with creating an isolated scope for your directive, you can also do this: 如果可以为指令创建隔离范围,则还可以执行以下操作:
app.directive('link', function() {
return {
restrict: 'E',
transclude: true,
replace: true,
scope: {
url: '@'
},
template: '<a href="{{url}}" ng-transclude></a>'
}
});
Then the url attribute will automatically get put into the scope of the directive. 然后,url属性将自动放入指令的范围内。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.