Can't load URL into iframe with angularJS

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-23 07:06:17

问题


Using AngularJS, Im trying to load the "myLink" URL address to iframe in another html. data.No is the id that i pull from another place and works fine (get the Id that i need for the url)

in the controller - "TransactionsCtrl":

$scope.myLink = "http://"the real url"+ data.No +"&file="+ data.No +"&contract_id="+ data.No;
console.log($scope.myLink);

in the HTML :

<div ng-controller= "TransactionsCtrl">
    <iframe ng-src="{{myLink}}"></iframe>
</div>

and all i get is this :

Error: [$interpolate:interr] Can't interpolate: {{myLink}}
Error: [$sce:insecurl] Blocked loading resource from url not allowed by $sceDelegate policy.  URL

when i hard coded the url its working fine.


回答1:


In the controller you should use:

   $scope.myLink =  $sce.trustAsResourceUrl(myUrl)



回答2:


It works for me : you can write this in js code as a function

$scope.trustSrc = function(src) {
  return $sce.trustAsResourceUrl(src);
}
$scope.iframe = {src:"http://www.youtube.com/embed/Lx7ycjC8qjE"};

and use it in your view :

 <iframe ng-src="{{trustSrc(iframe.src)}}"></iframe>

or you can write it as a filter like :

.filter('trusted', function($sce){
return function(url) {
    return $sce.trustAsResourceUrl(url);
};

})

and use it in view :

 <iframe ng-src="{{iframe.src | trusted}}"></iframe>


来源:https://stackoverflow.com/questions/26990014/cant-load-url-into-iframe-with-angularjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!