Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 530
Amila Sampath Avatar asked Aug 31 '15 10:08

Amila Sampath


2 Answers

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');
    }
}]);
like image 164
michelem Avatar answered Nov 15 '22 18:11

michelem


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>
like image 11
ShivangiBilora Avatar answered Nov 15 '22 17:11

ShivangiBilora