Able to access the Google Picker only once in a while. Google Picker Popup doesn't opens every time when I open the application.
I'm implementing Google Picker API in Angular 6. I kept separate file for the logic behind connecting the Google API in the assets folder of angular and by the help of document.createElement("script"), appended the javascript file. And I have an Anchor tag to getElementById in app.component.html.
app.component.html
<a routerLink="/" id="AllFilePick" #AllFilePick> Button </a>
app.component.ts
import { Component, ViewChild, ElementRef, OnInit } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
@ViewChild('AllFilePick') AllFilePick: ElementRef;
constructor(private elementRef: ElementRef) { }
ngOnInit() {
var s1 = document.createElement("script");
s1.type = "text/javascript";
s1.src = "../assets/api-script.js";
this.elementRef.nativeElement.appendChild(s1);
var s2 = document.createElement("script");
s2.src = "https://www.google.com/jsapi?key=<API_KEY>";
this.elementRef.nativeElement.appendChild(s2);
var s3 = document.createElement("script");
s3.src = "https://apis.google.com/js/client.js?onload=SetPicker";
this.elementRef.nativeElement.appendChild(s3);
// console.log(this.AllFilePick.nativeElement);
console.log(s1);
console.log(s2);
console.log(s3);
}
}
I even tried using ngAfterViewInit, constructor for appending the script tag.
assets/api-script.js
function SetPicker() {
var picker = new FilePicker(
{
apiKey: ‘<API_KEY>’,
clientId: ‘<CLIENT_ID>’,
buttonEl: document.getElementById("AllFilePick"),
onClick: function (file) {
PopupCenter('https://drive.google.com/file/d/' + file.id + '/view', "", 1026, 500);
}
}
);
}
function PopupCenter(url, title, w, h)
{
//.....
}
function FilePicker(User)
{
//Configuration
//....
}
The Above full version code runs properly but pop-up opens rarely, once in a while. Pop up triggers only after refreshing the application for several times or Opening the application next day. Picker doesn't works regularly in the Angular.