0

I am trying to display the youtube video of a specific ID that i can take out of my array. I use an expression to fill in this ID at the right place in my ng-src attribute , but it doesn't work. I used the same expression above the i-frame to see if the expression is working , and it did.

<iframe allowfullscreen="" 
        frameborder="0" 
        height="315" 
        ng-src="http://www.youtube.com/embed/{{selectedSong.youtube_ids.default}}?rel=0?rel=0&amp;hd=1" 
        width="560">
</iframe> 

As you see i used the {{selectedSong.youtube_ids.default}} expression for my id but when I inspect my html when running it is displaying the expression instead of the result (so I'm sure it isn't working).

4

3 Answers 3

3

As far as I know, this don't work because of security matters to previne xss attacks. But you can create a filter to trust the url like:

angular.module('filters', []).filter('trustThisUrl', ['$sce', function($sce) {
    return function(val) {
        return $sce.trustAsResourceUrl(val);
    };
}]);

And then call it as filter of the url in src

<iframe ng-src="{{ anUrlFromYourController | trustThisUrl}}"></iframe>
Sign up to request clarification or add additional context in comments.

6 Comments

I have added this filter , but now i'm trying to figure out how i have to declare the 'anUrlFromYourController' in my expression i have tried something like this ng-src={{"youtube.com/embed" + selectedSong.youtube_ids.default + "?rel=0?rel=0&amp;hd=1" | trustThisUrl}}"
You should try to build the full URL in your controller to avoid that concat in the ng-src. I don't know if it is a bad practice, but if you build the URL in the controller, your code gets cleaner, more readable and you can get the filter to work properly because it evaluate an variable value.
I am so sorry to bug you with this but I'm a beginner in angular.js and javascript , can u maybe explain me how i build this URL in my controller?
You're no bugging me at all. Well, considering that your iframe is inside an ng-repeat, the selectedSong is an element of an array, right? So, you can iterate that array on your controller before you pass it to the page. Something like: for(var i in songs){ songs[i].fullUrl = "http://www.youtube.com/embed/"+song[i].youtube_ids.default+"?rel=0?rel=0&amp;hd=1 "} and then put the songs array into an $scope variable to use in the ng-repeat. After that just call the fullUrl as an attribute, like: ng-src={{selectedSong.fullUrl | trustThisUrl}}
I'm still confused of using the $scope variable and such , as i said i am a very big noob and this javascript looks very advanced , can i maybe ask a bit of your time to look at my application on Plunkr ? this is the link : plnkr.co/edit/SJHNbzhQXIpYxifWBKuh?p=preview
|
1

I have found a better solution for this where the bug is resolved:

Alternative to iFrames with HTML5

Comments

0

in your controller : insert in parameter : $sce

function($sce, ...)

$scope.linkYoutube = $sce.trustAsResourceUrl('http://www.youtube.com/embed/'+data['yourLink']);

Then in the html, you can use `ng-src="linkYoutube"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.