我尝试从this fiddle动态添加meta标签,我尝试了this answer。
页:
<html ng-app="meumobiApp" ng-controller="SiteCtrl">
<head>
<smart-banner></smart-banner>
</head>
<body></body>
<html>
和指令:
module.directive("smartBanner",function(){
return {
restrict: "E",
template: '<meta name="apple-itunes-app" content=""></meta>',
replace: true,
link: function(scope) {}
}
});
但是该标签将插入到身体标签中,而不是头部中。
可以将标签插入头部,还是我必须尝试其他方法?
最佳答案
在您的指令链接函数中,您应该这样做
module.directive("smartBanner",function(){
return {
restrict: "E",
template: '<meta name="apple-itunes-app" content=""></meta>',
replace: true,
link: function(scope) {
var metaTag=document.createElement('meta');
metaTag.name = "apple-itunes-app";
metaTag.content = "";
document.getElementsByTagName('head')[0].appendChild(metaTag);
}
}
});
但您也可以在没有指令的情况下执行此操作,但是指令也很好
关于javascript - 如何使用指令将meta标签插入头部,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25266638/