Javascript 如何更改 iframe src 使用 angularjs

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32309183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:51:59  来源:igfitidea点击:

How to change iframe src use angularjs

javascriptangularjs

提问by Amila Sampath

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

How to change iframe src

如何更改 iframe src

回答by michelem

You need also $sce.trustAsResourceUrlor 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');
    }
}]);

回答by ShivangiBilora

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:

为什么不将源更改为 ng-src。这样,您可以将 src 链接到一个变量,并按如下方式更改它:

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