Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using string concatenation within HTML img src in AngularJS

I am pulling videos from a playlist with the YouTube API v3. Each video JSON object has a videoId. I am trying to use the videoId to build the src attribute in the img element with Angular.

This is what I currently have:

<table id="playlistvideostable" class="table table-responsive table-striped">
    <thead>
        <tr>
            <th>Name</th>
            <th>Thumbnail</th>
        </tr>
    </thead>
    <tbody>
        <tr ng-repeat="video in playlistvideos">
            <td>
                {{video.snippet.title}}
            </td>
            <td>
                <img src="http://img.youtube.com/vi/{{video.resourceId.videoId}}/hqdefault.jpg" alt="Video Thumbnail" class="img-responsive"/>
            </td>
        </tr>
    </tbody>
</table>

How can I concatenate the following strings in the src attribute of the img html element?

"http://img.youtube.com/vi/" + video.resourceId.videoId + "/hqdefault.jpg"

like image 704
user3788671 Avatar asked Nov 10 '15 18:11

user3788671


People also ask

Can you concatenate strings in HTML?

HTML is a markup language. No, it has no 'concatenation functionality'.

How do you concatenate variables in HTML?

To concatenate a string, you add a plus sign+ between the strings or string variables you want to connect. let myPet = 'seahorse'; console. log('My favorite animal is the ' + myPet + '.

Can you concatenate strings in JavaScript?

The concat() function concatenates the string arguments to the calling string and returns a new string.


1 Answers

use the ng-src directive instead of the original src attribute of the image element, the ng-src directive will recognize the string interpolation and apply it on the dom.

in your case -

  <img ng-src="http://img.youtube.com/vi/{{video.resourceId.videoId}}/hqdefault.jpg" alt="Video Thumbnail" class="img-responsive"/>

good luck.

like image 146
Ran Sasportas Avatar answered Oct 21 '22 21:10

Ran Sasportas