【发布时间】:2015-08-31 10:29:44
【问题描述】:
<iframe src="{{url}}" width="300" height="600">
<p>Your browser does not support iframes.</p>
</iframe>
如何更改 iframe src
【问题讨论】:
标签: javascript angularjs
<iframe src="{{url}}" width="300" height="600">
<p>Your browser does not support iframes.</p>
</iframe>
如何更改 iframe src
【问题讨论】:
标签: javascript angularjs
您还需要$sce.trustAsResourceUrl,否则将无法在 iframe 中打开网站:
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');
}
}]);
【讨论】:
为什么不将源代码更改为 ng-src。这样,您可以将 src 链接到一个变量,并进行如下更改:
<iframe ng-src="{{url}}"> <p>Your browser does not support iframes.</p> </iframe>
【讨论】: