Angular Js and google api client.js (gapi)

后端 未结 8 1514
不思量自难忘°
不思量自难忘° 2020-11-29 00:46

It took me one day to make it works so I think my experience may be useful from someone. And maybe some others will find improvement.

So I start angularJS two days a

8条回答
  •  隐瞒了意图╮
    2020-11-29 01:24

    So I was having the same problem. Putting this code in my factory worked

    var initialize = function() {
        if(gapi.client == undefined) {
            setTimeout(function() {
                initialize()
            }, 1000);
        } else {
            gapi.client.setApiKey("");
            gapi.client.load('youtube', 'v3').then(function() {
                console.log("youtube is ready")
            });
        }
    };
    
    initialize()
    

    Basically, the problem is trying to call gapi.client before it loaded. If you just check that it's loaded, and if it isn't then try again in a second (you can set the time for whatever you want, set it lower if you expect the user to need this relatively quickly after the page loads).

    I was struggling with this for a while, and this is all that worked for me...Hope this helps!

提交回复
热议问题