javascript - $window 错误 '$window not defined' Angular 应用程序中的谷歌分析代码

标签 javascript jquery angularjs google-analytics

当尝试运行 GA (谷歌分析代码)在我的 Angular 应用中。

错误:$window 未定义”

   .run(function($rootScope, APIServer, social, $location, music, $anchorScroll, $routeParams) {

        // $window.ga('create', 'UA-55555555-5', 'auto'); <-- here

        /*
         * Scroll down to comments if available
         */
        $anchorScroll.yOffset = 70;
        //when the route is changed scroll to the proper element.
        $rootScope.$on('$routeChangeSuccess', function(newRoute, oldRoute) {
            // $window.ga('send', 'pageview', $location.path()); <-- here
            $location.hash($routeParams.scrollTo);
            $anchorScroll();  
        });

最佳答案

也注入(inject) $window。

.run(function($rootScope, APIServer, social, $location, music, $anchorScroll, $routeParams, $window) {

        // $window.ga('create', 'UA-55555555-5', 'auto'); <-- here

        /*
         * Scroll down to comments if available
         */
        $anchorScroll.yOffset = 70;
        //when the route is changed scroll to the proper element.
        $rootScope.$on('$routeChangeSuccess', function(newRoute, oldRoute) {
            // $window.ga('send', 'pageview', $location.path()); <-- here
            $location.hash($routeParams.scrollTo);
            $anchorScroll();  
        });

关于javascript - $window 错误 '$window not defined' Angular 应用程序中的谷歌分析代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36238336/

相关文章:

javascript - 段落形式的随机列表

javascript - 为什么建议为 parseInt() 提供可选的基数参数?

javascript - 如何在鼠标悬停表格行上的鼠标旁边显示图像

angularjs - $q 和 $http 可以注入(inject) .config 部分吗

javascript - Angular JS, 'nomod',模块 '{0}' 不可用!你要么拼错

javascript - 动态字段上的 AJAX 自动完成

javascript - 单击 div 的滚动条会触发 I.E 中的模糊事件

php - Laravel 5.6 Ajax 发布数据

jQuery onClick 转到 ID 或类

javascript - 为什么我的 ng-materials "select"元素的功能与 HTML5 元素不同?