Thursday, April 25, 2024
 Popular · Latest · Hot · Upcoming
rated 0 times [  60] [ 4]  / answers: 1 / hits: 78992  / 11 Years ago, thu, november 7, 2013, 12:00:00

Using Angular 1.2+, I'm trying to work out an angular way to load in an iFrame but I can't find any tutorials/any real discussion on this anywhere.

Basically, I have a search page which displays a list of links. Clicking a link should call a function in my controller which loads in data (possibly through the $http service?) to an iFrame while keeping the search results in view.

Here's some rudimentary code (I've never really used iFrames before so apologies if this is just totally wrong)


<div ng-controller=searchPage>
<div ng-repeat='post in searchResults'>
<a ng-click=itemDetail(post._infoLink)>Here's a link!</a>
<div class=detailView ng-show=itemShown>
<iframe ng-src=detailFrame></iframe>


$scope.itemDetail = function(link){
$scope.itemShown = true;
$scope.busy = true;
$scope.busy = false;
$scope.detailFrame = data;

This code currently gives the error:

 XMLHttpRequest cannot load <url>. Origin http://localhost:5000 is not allowed by Access-Control-Allow-Origin. 

Something simple along these lines would be ideal. In the (attempted) example above, the search results would stay on the screen and get pushed into a sidebar, while the iFrame loads in an external site (on a different domain) into the bulk of the page.

How can this be achieved with Angular?

Extra caveat: The links to load into the iFrame are affiliate links so they'll often have a 'middleman' domain between them, e.g. the clicked-link will be which redirects to which then leads to the final site,

More From » angularjs


Due to Same Origin Policy, you can't get cross domain data by using XMLHttpRequest.

I'm not quite sure that what you really want to do.

You could try to bind iframe src attribute and clicked link url together to achieve the function if you just want iframe to show the web page which user cliked.

I wrote a simple example:


<div ng-app ng-controller=searchPage>
<div ng-repeat=page in searchResults>
<a ng-click=itemDetail(page._infoLink)>{{page.label}}</a>
<iframe width=500 height=400 ng-src={{detailFrame}}></iframe>


function searchPage($scope){
$scope.searchResults = [{label:BBC,_infoLink:},{label:CNN,_infoLink:}];

$scope.itemDetail = function(link){
$scope.detailFrame = link;

Here is the jsFiddle demo

BTW... You could try server proxy if you really want to use XMLHttpRequest to grab data from 3-party web site and dump it to somewhere (You can't dump data to iframe src attribute! It only accepts the URL of web page).

[#74446] Wednesday, November 6, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.

Total Points: 185
Total Questions: 99
Total Answers: 87

Location: Sao Tome and Principe
Member since Wed, Dec 21, 2022
1 Year ago