I'm trying to add Google Custom Search into my angular 2 application. Using the code from the custom search works when I put it into a jsfiddle, but I'm having issues getting it to work when inserted into my component.
The issue seems to be, that by the time the code to insert the script runs, the html tag <gcse:search>
is stripped of it's gcse:
part to become <search>
and I guess the script that runs then can't find any elements to work on.
My.component.html is essentially:
<gcse:search></gcse:search>
and in My.component.html.ts I have a function which implements ngOnInit
ngOnInit(){
var cx = '016820916711928902111:qw0kgpuhihm';
var gcse = document.createElement('script');
gcse.type = 'text/javascript';
gcse.async = true;
gcse.src = 'https://cse.google.com/cse.js?cx=' + cx;
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(gcse, s);
}