0

JSON API

teams": [

    {
        "logo": "/wp-content/themes/squiggle/assets/images/Adelaide.jpg",
        "abbrev": "ADE",
        "name": "Adelaide",
        "id": 1
    },

component.ts

  teams = [];

  responseData: any

constructor(private http: HttpClient) { }

ngOnInit(): void {
    this.http.get("https://api.squiggle.com.au/?q=teams").subscribe((res: any) => {
      this.responseData = JSON.parse(JSON.stringify(res))["teams"];
    },
      err => {
        console.log(err);
      },
      () => {
        console.log(this.responseData);
      }
    )
  }

HTML File

 <tr *ngFor="let team of responseData">
          <td>{{team.id}}</td>
          <td>{{team.abbrev}}</td>
          <td><img [src]="{{team.logo}}" alt="image"</td>
          <td>{{team.name}}</td>
 </tr>

The HttpClient is working fine and display it perfectly except of the images. The image are being displayed as "/wp-content/themes/squiggle/assets/images/Adelaide.jpg". I have tried using img tag with src[] but does not seem to fix.

0

3 Answers 3

2

It appears the logos are under the domain https://squiggle.com.au whereas the API is under https://api.squiggle.com.au/. So you might have to manually concatenate the domain for each element of the array. I've modified the data extraction as well using RxJS pipe and map. Try the following

IMG_BASE_URL = 'https://squiggle.com.au';

ngOnInit() {
  this.http.get("https://api.squiggle.com.au/?q=teams")
    .pipe(map(teams => {
      const result = teams['teams'];
      result.forEach(team => team['logo'] = this.IMG_BASE_URL + team['logo'] );
      return result;
    }))
    .subscribe(
      res => { this.responseData = res; },
      err => { console.log(err); },
      () => { console.log(this.responseData); }
    );
}

Also the URL binding to img should either be using data binding syntax [src]="team.logo" or data interpolation syntax src="{{team.logo}}". They shouldn't be mixed together, please notice the square brackets.

Template

<tr *ngFor="let team of responseData">
  <td>{{team.id}}</td>
  <td>{{team.abbrev}}</td>
  <td><img [src]="team.logo" alt="image"></td>
  <td>{{team.name}}</td>
</tr>
Sign up to request clarification or add additional context in comments.

2 Comments

works perfect, any tutorial you would recommend related to these types of topics?
TBH the logo domain was a bit of guesswork. If you haven't gone through already, Angular tutorial (angular.io/tutorial) is a quick and good introduction to some basics of Angular.
0

im not sure this is it but you shouldnt use "[]" and "{{}}" try switching [src]="{{team.logo}}" for [src]="team.logo"

Comments

0

you don't need interpolation with attribute binding.

<td><img [src]="team.logo" alt="image"</td>

should do the job.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.