Whats equivalent to ngSrc in the newer Angular?

Angular

Angular Problem Overview


I would like to implement img, with a src coming from JSON object.

In AngularJS, I could do:

<img ng-src="{{hash}}" alt="Description" />

Is there any equivalent to this in Angular 2+?

Angular Solutions


Solution 1 - Angular

AngularJS:

<img ng-src="{{movie.imageurl}}">

Angular 2+:

<img [src]="movie.imageurl">

Angular docs


Note that interpolation can achieve the same result:

<img src="{{vehicle.imageUrl}}">

<img [src]="vehicle.imageUrl">

There is no technical difference between these two statements for property binding, as long as you don't desire two-way binding.

> Interpolation is a convenient alternative for property binding in many > cases. In fact, Angular translates those interpolations into the > corresponding property bindings before rendering the view. source

Solution 2 - Angular

It is a two-step process to achieve the same functionality of ng-src in your Angular application.

First Step:

In your HTML, use the new syntax:

<img [src]="imageSrc">

Second Step:

In your component/directive, initialize the value to be empty. For example:

@Component({
  selector: 'ag-video',
  templateUrl: './video.component.html'
})
export class SampleComponent {
   imageSrc = '';
}

Now, this would eliminate the null network call (empty call) due to the value not being set on to the element.

Solution 3 - Angular

It can also be written in interpolation form like:

<img src="{{movie.imageurl}}">

Solution 4 - Angular

<tr *ngFor="let post of posts | paginate: { id: 'server', itemsPerPage: 
       10, currentPage: p , totalItems: count  }">

   <td>
    <img src="{{post.youtubeVideoId}}">
   </td>

   <td>
     {{post.videoName}}
   </td>

</tr>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionukszView Question on Stackoverflow
Solution 1 - AngularRandyView Answer on Stackoverflow
Solution 2 - AngularNikhilesh ShivarathriView Answer on Stackoverflow
Solution 3 - AngularRaksha BhartiyaView Answer on Stackoverflow
Solution 4 - AngularShashwat GuptaView Answer on Stackoverflow