Ways to programmatically insert an iframe source using Angular

I am encountering an issue with dynamically embedding a YouTube video into an iframe, specifically an

Error: $interpolate:interr Interpolation Error
.

Here is the HTML code:

<div class="embed-responsive embed-responsive-16by9">
    <iframe class="embed-responsive-item" src="{{youtubeVideo}}" allowfullscreen></iframe>
</div>

And here is the JavaScript/Angular code:

$http({
    method: 'GET',
    url: getTankVideos(query)
}).then(function successCallback(response) {
    console.debug('response');
    console.debug(response);

    $scope.youtube = response;
    console.log('Youtube');
    console.log($scope.youtube);

    console.log('Video Url');
    $scope.youtubeVideo = 'https://www.youtube.com/watch?v=' + $scope.youtube.data.items[0].id.videoId;
    console.log($scope.youtubeVideo);

}, function errorCallback(response) {
    // Handle errors here
});

The debug statements show valid values being returned:

response
script.js:129 Object {data: Object, status: 200, config: Object, statusText: ""}
script.js:131 Youtube
script.js:132 Object {data: Object, status: 200, config: Object, statusText: ""}
script.js:134 Video Url
script.js:135 https://www.youtube.com/watch?v=BRtj_TSOHjw

Answer №1

To ensure security measures, it is important to include the $sce service in the controller and trustAsResourceUrl the URL within that function.

$scope.youtubeVideo = $sce.trustAsResourceUrl('https://www.youtube.com/watch?v=' + $scope.youtube.data.items[0].id.videoId);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using Leaflet to make geojson XMLHttpRequests

Recently, I've been exploring the world of JavaScript and attempting to map around 2,200 data points in Leaflet. Despite following a helpful tutorial on pulling data from a geojson file and displaying it on a map, I can't seem to make it work wit ...

The div structure generated through JavaScript appears distinct from its representation in the live DOM

Currently, I am facing a challenge with creating a complex nested Div structure within a JavaScript loop that iterates over JSON response objects from the Instagram API. The main goal is to set the URL for each image within a specific Bootstrap div layout. ...

Is there a way to trigger jQuery animations even when the browser window is not in focus?

Hi there, I am currently working on developing an ajax-based multiplayer game that connects to a server for updates. The game has various game states and requires the GUI to be animated accordingly. However, I have encountered a problem where jquery anima ...

Undefined Variables in AngularJS Directive Scopes

Check out this JSFiddle I created https://jsfiddle.net/9Ltyru6a/3/ I set up a controller and a directive in the fiddle to trigger a callback when a value is changed. Instead of using the ng-change directive in Angular, I wanted to create an event similar ...

Interactive Thumbnail Selection for HTML5 Video

Having trouble with creating thumbnails? I managed to solve the cross-domain issue using an html2canvas PHP proxy. No error messages in the Console, but the thumbnails are unfortunately not showing up - they appear transparent or white. Here is a snippet ...

A guide on utilizing the TypeScript compilerOptions --outDir feature

Recently, I encountered an error message from the compiler stating: Cannot write file 'path/file.json' because it would overwrite input file. After some investigation, most of the solutions suggested using outDir to resolve this issue. Although t ...

Adding the AJAX response to the specified element's ID

I'm having trouble inserting radio buttons into a <div> on my page using an AJAX response. The code I have looks like this: // AJAX form for updating tests after category selection $('#categories').change(function(){ category_id ...

Updating the React state of a function that is enclosed inside useRef does not result in any changes

I am facing an issue with accessing React state within a function that is enclosed in a useRef. Despite trying to use a helper function bound to App to access the state, the state does not update as expected inside the useRef function. getCount outside 0 ...

Launching an Electron app with an Express server in Node.js

I have a program using Express/Nodejs for the backend, and AngularJS (I know it's old) for the frontend. I'm attempting to display this program in an Electron window. After some searching, here's what I've found: main.js const {Browser ...

getting data from JavaScript function by making an asynchronous request to RESTful API

My current challenge involves calling a JavaScript method that utilizes AJAX to access a REST service and then return the response to the original call. While this may seem straightforward, I have scoured Stack Overflow for answers but haven't found a ...

Is there a way to apply filters to jquery datatables during initialization process?

Is there a way to server-side filter datatable during initialization? I attempted the following code: function tableFilter(arg1, param2, value3) { var searchTable = $("#tblsearch").dataTable({ "bRetrieve": true, "b ...

Deactivated the UpdateProgress functionality for the entire webpage

After exploring various options, I came across this solution: Is there a way to disable UpdateProgress for certain async postbacks? However, implementing this solution seems to prevent my controls from loading altogether! In my master page, there is an U ...

Navigating with fun! 2 in scala following a submission using angularJs

Up to this point, I've been using the traditional method of submitting a form and redirecting to the desired page like so: The HTML section : <form name="createArtistForm" method="post" action="/admin/createArtist"> Artist Name : <input ...

Adjust the height of a div element and enable scrolling to fit the

There are numerous inquiries regarding this particular issue. I have exhausted nearly all possible solutions I could find, yet none of them delivered the desired outcome. This is the structure of my page: <div> <header id="status"> / ...

Leveraging .tsx components within nested .tsx components in React Native

Currently, I am delving into the world of building apps using TypeScript in React Native. Coming from a background as a Swift developer, adjusting to JavaScript and TypeScript has been an interesting journey. An observation that stood out to me is the cha ...

Discover the process of dynamically importing JavaScript libraries, modules, and non-component elements within a Next.js

Lately, I have been utilizing Next.js and mastering its dynamic import feature for importing components with named exports. However, I recently encountered a particular npm package that functions only on the client-side (requires window) and has a substant ...

Add option button

Is there a way to dynamically add radio buttons using jQuery or JavaScript and save the data into a database? I have successfully appended input types such as text, textarea, checkbox, and select with options. Here is my code: <!DOCTYPE html> < ...

A guide on toggling a div in Ionic3

Looking to improve user experience, I need to display a long text partially with an 'expand' button. Clicking the button will reveal the full text. User interface design: Default view: https://i.sstatic.net/whJDN.png After expansion: https:/ ...

How about wrapping a large amount of text three times and including a "read more" link?

I am tasked with creating an HTML element that automatically detects when text has wrapped three times, truncates the content, and adds a "more..." link at the end of the third line. Can this be achieved? If yes, what is the process to implement it? ...

Problem with BehaviorSubject: next() method is not functioning as expected

My goal is to utilize an angular service for storing and communicating data via observables. Below is the implementation of my service: public _currentLegal = new BehaviorSubject({ name: 'init', _id: 'init', country: 'init& ...