简体   繁体   中英

Fetch url locally in angular

I am working with simple Angular project. I have JSON file of data

[{
"name": "Little Collins",
"area": "Bronx",
"city": "New York",
"coverImage": "https://images.unsplash.com/photo-1576808597967-93bd9aaa6bae?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1241&q=80"}]

This is the interface i am using

export interface IRestaurant{
name: string,
area: string,
city: string,
coverImage: string}

But i am not able to load that cover image url. I am getting empty list.

<ul *ngFor="let restaurant of restaurants">
<li>{{restaurant.name}} - {{restaurant.area}} - {{restaurant.city}}</li>
<li>{{restaurant.coverImage}}</li>

图像文件不是直接在 HTML 中加载的,因此您需要<img>标签来加载图像,如下所示..

<img src="{{restaurant.coverImage}}" />

Try like this:

<li>
    <img [src]="restaurant.coverImage" />
</li>

Working Demo

Image file cannot load directly, so you need tag :

  <ul *ngFor="let restaurant of restaurants">
        <li>
            <img [src]="restaurant.coverImage" height="300"/>
        </li>
  </ul>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM