Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embedding twitter timeline does not render in angular 7

I am following https://help.twitter.com/en/using-twitter/embed-twitter-feed for embedding timeline in the angular page. Only button renders but not the actual timeline.

The index.html looks like:

<body style="margin:0">
    <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
    <app-root></app-root>
</body>

app.component.html looks like below:

<a class="twitter-timeline"
   href="https://twitter.com/TwitterDev/lists/national-parks?ref_src=twsrc%5Etfw">
      A Twitter List by TwitterDev
</a> 

Also tried things like app.component.ts:

ngOnInit(){
   if ((<any>window).twttr.ready())
      (<any>window).twttr.widgets.load();
}

But no luck

like image 520
Moblize IT Avatar asked Jun 13 '19 05:06

Moblize IT


3 Answers

I had a requirement of dynamically rendering timelines based on different twitter timelines.

I found a workaround by creating a variable in the constructor that stores the href based on the twitter username .

So for example if your link is "https://twitter.com/TwitterDev/lists/national-parks?ref_src=twsrc%5Etfw" , you just put this in the constructor in a previously defined global variable , say "embedLink"

such as in your ts component:


@Component({
  selector: 'app-tree-dashboard',
  templateUrl: './tree-dashboard.component.html',
  styleUrls: ['./tree-dashboard.component.css']
})
export class TreeDashboardComponent implements OnInit,AfterViewInit {
  embedLink='';

  constructor(private matIconRegistry: MatIconRegistry,
              ) {
this.embedLink=  "https://twitter.com/TwitterDev/lists/national-parksref_src=twsrc%5Etfw" 
}};

and then in your HTML :

<a class="twitter-timeline" href={{embedLink}}></a>

And lastly you only need to add the script in index.html which you have done already. So you're good to go!

like image 21
Deep Dhillon Avatar answered Sep 19 '22 11:09

Deep Dhillon


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 ๐Ÿ’ฅ๐Ÿ’ฅ

like image 159
Muhammed Albarmavi Avatar answered Sep 18 '22 11:09

Muhammed Albarmavi


Below is my code. I'm creating blank website so I think it's should not be a problem. What I think is maybe the order of the script in index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title>Twitter</title>
    <base href="/" />

    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link rel="icon" type="image/x-icon" href="favicon.ico" />
  </head>
  <body>
    <app-root></app-root>
  </body>
  <script
    async
    src="https://platform.twitter.com/widgets.js"
    charset="utf-8"
  ></script>
</html>

In my app.component.html

<a class="twitter-timeline"
   href="https://twitter.com/TwitterDev/lists/national-parks?ref_src=twsrc%5Etfw">
      A Twitter List by TwitterDev
</a>

You can view my code here

like image 24
Tony Ngo Avatar answered Sep 22 '22 11:09

Tony Ngo