you need to load widgets.js
script after twitter-timeline
element is been render so if you place the script in index.html it is will load and the element hasn't render yet.
the best way around it is to create a script tag dynamically after the element is rendered.
twitter component
export class TwitterComponent {
@Input() user:string;
constructor(private renderer2: Renderer2,private el: ElementRef) {}
ngAfterViewInit() {
let scriptEl = document.createElement('script');
scriptEl.src = "https://platform.twitter.com/widgets.js"
this.renderer2.appendChild(this.el.nativeElement, scriptEl);
}
}
template
<a class="twitter-timeline" href="https://twitter.com/{{user}}">Tweets by {{user}}</a>
app componenet template
<app-twitter [user]="name"></app-twitter>
angular twitter widgets ⚡⚡
ngAfterViewInit() a lifecycle hook that is called after Angular has fully initialized a component's view.
Updated
a simple soulution mention in this answer before by user named Bernardo Baumblatt
put the script link in the index.html
<script async src="https://platform.twitter.com/widgets.js" charset="utf-8">
</script>
load the twitter widgets when ngAfterViewInit method call
ngAfterViewInit() {
// @ts-ignore
twttr.widgets.load();
}
in any case the script has not loaded yet you will got an error like twttr is not defined
so download the widgets.js
script and include it to your project by using import
main.ts
import './app/widgets.js'
demo