Amila Sampath
Amila Sampath

Reputation: 645

How to change iframe src use angularjs

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

How to change iframe src

Upvotes: 15

Views: 44138

Answers (2)

michelem
michelem

Reputation: 14590

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');
    }
}]);

Upvotes: 41

Shivangi
Shivangi

Reputation: 3062

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>

Upvotes: 11

Related Questions