简体   繁体   English

如何更改iframe src使用angularjs

[英]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 如何改变iframe src

You need also $sce.trustAsResourceUrl or it won't open the website inside the iframe: 您还需要$sce.trustAsResourceUrl ,否则它将无法在iframe中打开网站:

JSFiddle 的jsfiddle

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

Why don't you change the source to ng-src. 为什么不将源更改为ng-src。 That way, you may link the src to a variable, and change it as follows: 这样,您可以将src链接到变量,并按如下所示进行更改:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM