15
<iframe src="{{url}}" width="300" height="600">
                <p>Your browser does not support iframes.</p>
            </iframe>

How to change iframe src

2 Answers 2

41

You need also $sce.trustAsResourceUrl or it won't open the website inside the iframe:

JSFiddle

HTML:

<div ng-app="myApp" ng-controller="dummy">
    <button ng-click="changeIt()">Change it</button>
    <iframe ng-src="{{url}}" width="300" height="600"></iframe>
</div>

JS:

angular.module('myApp', [])
    .controller('dummy', ['$scope', '$sce', function ($scope, $sce) {

    $scope.url = $sce.trustAsResourceUrl('https://www.angularjs.org');

    $scope.changeIt = function () {
        $scope.url = $sce.trustAsResourceUrl('https://docs.angularjs.org/tutorial');
    }
}]);
Sign up to request clarification or add additional context in comments.

1 Comment

I think this should be considered as the correct answer
11

Why don't you change the source to ng-src. That way, you may link the src to a variable, and change it as follows:

<iframe ng-src="{{url}}">  <p>Your browser does not support iframes.</p> </iframe>

2 Comments

works only for image. need to use $sce.trustAsResourceUrl
@KamalKumar Yeah you need to use $sce but without ng-src you will get a bunch of console errors and bad requests.

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.