【发布时间】:2016-06-16 15:06:09
【问题描述】:
我正在尝试将我的谷歌分析脚本注入我的应用程序。我在 index.html 文件的头部有硬编码
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
</script>
这是我的指令:
app.directive('googleAnalytics', function(configFactory){
return {
restrict: 'E',
replace: true,
templateUrl: '../views/gaScript.html',
link: function(scope,element,attrs){
configFactory.getconfigs().then(function(configs) {
scope.gid = configs[0].ga_id;
});
}
};
})
该指令在 gaScript.html 中使用此模板来注入正确的 ga ID
<script>ga('create', '{{gid}}', 'auto');</script>
这是正文中被指令替换的 html 标记
<google-analytics></google-analytics>
问题是我认为页面正在将 <script>ga('create', '{{gid}}', 'auto');</script> 作为字符串读取,而不是解释脚本标签
【问题讨论】:
-
'../views/gaScript.html'中有什么内容? -
@mparnisari 这是里面 gaScript.html
<script>ga('create', '{{gid}}', 'auto');</script> -
您使用的是哪个版本的 Angular?
-
Angular 版本 1.4.7
标签: javascript angularjs google-analytics