本文介绍了Google Charts Embed API无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Embed API在ServiceAccount上的网站上添加GA图表.该站点是带有AngularJS的单页应用程序.我为每种图表类型和一个控制器创建了多个指令,将其命名为GAController,并在它们之间共享.在此控制器中,我使用gapi.analytics.ready(..)等到完全加载分析对象.

I'm using Embed API to include GA charts on my site from ServiceAccount.The site is a Single Page application with AngularJS.I've created several directives for every chart type and a controller, name it GAController, shared between them. In this controller i use gapi.analytics.ready(..) to wait until analytics object is fully loaded.

问题在于页面刷新后第一次调用GAController时,然后使用回调函数调用gapi.analytics.ready(..),然后执行身份验证和其他逻辑.管理员,如果我第二次访问此页面,从其他页面通过ui-router,然后将回调添加到ready,但未执行.完全没有.

The problem is that when GAController is called for first time after page refresh, then gapi.analytics.ready(..) is called with callback function and then authentication and other logic executes.Hovewer, if i go to this page for the second time f.e. from other page via ui-router, then callback is added to ready but not executed. At all.

在模板页面底部,有gapi script:

  (function(w,d,s,g,js,fjs){
g=w.gapi||(w.gapi={});g.analytics={q:[],ready:function(cb){this.q.push(cb)}};
js=d.createElement(s);fjs=d.getElementsByTagName(s)[0];
js.src='https://apis.google.com/js/platform.js';
fjs.parentNode.insertBefore(js,fjs);js.onload=function(){g.load('analytics')};}
(window,document,'script'));

我的代码如下:

gapi.analytics.ready(function () {
  // this is not called the second time
  if (!gapi.analytics.auth.isAuthorized()) {
    gapi.analytics.auth.authorize({
      serverAuth: $scope.$parent.getAccessToken()
    });
  }
  // do some other logic
});

,但gapi.analytics对象始终处于相同的状态{ q: [], ready: function()...}.

but all the times gapi.analytics object is in the same state { q: [], ready: function()...}.

有人可以帮助我了解发生了什么事吗?感谢您的帮助

Can somebody help me to understand what's going on.Thanks for any help

推荐答案

我找到了答案.您可以尝试一下,替换"gapi脚本"

I found the answer.You can try this, alternate the "gapi script"

                    (function (w, d, s, g, js, fs) {
                    if ($('#googleCache').length > 0) return;
                    g = w.gapi || (w.gapi = {}); g.analytics = { q: [], ready: function (f) { this.q.push(f); } };
                    js = d.createElement(s); fs = d.getElementsByTagName(s)[0];
                    js.src = 'https://apis.google.com/js/platform.js';
                    js.id = "googleCache";
                    fs.parentNode.insertBefore(js, fs); js.onload = function () { g.load('analytics'); };
                }(window, document, 'script'));

这篇关于Google Charts Embed API无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 14:35