AngularJS iframe跨域打开内容时报差错的解决办法

最近在angular中发现了一个 跨域的问题,

在angular框架中使用 iframe标签通过ng-src打开不同域名下的资源会发生跨域情况

解决办法即:通过$sceDelegateProvider绑定白名单,代码如下

<body>
	<div ng-app='myapp' ng-controller='myCtrl'>
		<iframe ng-src="{{url}}" width="" height=""></iframe>
	</div>
</body>
	<script src="http://cdn.static.runoob.com/libs/angular.js/1.4.6/angular.min.js"></script>
	<script src="http://apps.bdimg.com/libs/angular-route/1.3.13/angular-route.js"></script>
	<script type="text/javascript">
		var app = angular.module('myapp', ['ngRoute']);
			app.config(['$sceDelegateProvider',function($sceDelegateProvider) {
				$sceDelegateProvider.resourceUrlWhitelist([
					'self',
					'https://www.baidu.com/**'
				]);
		}]);
		app.controller('myCtrl', function($scope) {
			$scope.url = 'https://www.baidu.com'
		})
</script>

猜你喜欢

转载自blog.csdn.net/qq_36725757/article/details/80364620