如何更改iframe src使用angularjs

时间:2015-08-31 10:29:45

标签: javascript angularjs

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

如何更改iframe src

2 个答案:

答案 0 :(得分:37)

您还需要$sce.trustAsResourceUrl或者它不会在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');
    }
}]);

答案 1 :(得分:11)

为什么不将源更改为ng-src。这样,您可以将src链接到变量,并按如下所示进行更改:

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