如何从 AngularJS 中的变量设置 iframe src 属性 [英] How to set an iframe src attribute from a variable in AngularJS
问题描述
我正在尝试从变量设置 iframe 的 src
属性,但我无法让它工作...
标记:
<ul class=""><li ng-repeat="项目中的项目"><a ng-click="setProject(project.id)" href="">{{project.url}}</a><iframe ng-src="{{trustSrc(currentProject.url)}}">有问题...</iframe>
控制器/app.js:
function AppCtrl ($scope) {$scope.projects = {1:{身份证":1,"name" : "梅拉萨卡","url": "http://blabla.com",描述":麦吉尔大学教授梅拉萨卡尔的专业作品集网站."},2:{身份证":2,"name" : "好看","url": "http://goodwatching.com",描述":周末实验,帮助我妈妈决定看什么."}};$scope.setProject = 函数 (id) {$scope.currentProject = $scope.projects[id];console.log( $scope.currentProject );}}
使用此代码,不会在 iframe 的 src
属性中插入任何内容.只是一片空白.
更新 1:我将 $sce
依赖注入到 AppCtrl 中,$sce.trustUrl() 现在可以正常工作而不会抛出错误.但是它返回 TrustedValueHolderType
,我不确定如何使用它来插入实际的 URL.无论我在属性 src="{{trustUrl(currentProjectUrl))}}"
的插值大括号内使用 $sce.trustUrl() 还是在控制器内执行时,都会返回相同的类型设置 currentProjectUrl 的值.我什至都试过.
更新 2:我想出了如何使用 .toString() 从 trustUrlHolder 返回 url,但是当我这样做时,当我尝试将其传递到 src 属性时,它会抛出安全警告.
更新 3:如果我在控制器中使用 trustAsResourceUrl() 并将其传递给 ng-src 属性中使用的变量,则它会起作用:
$scope.setProject = function (id) {$scope.currentProject = $scope.projects[id];$scope.currentProjectUrl = $sce.trustAsResourceUrl($scope.currentProject.url);console.log( $scope.currentProject );console.log( $scope.currentProjectUrl );}
我的问题似乎通过这个解决了,虽然我不太确定为什么.
我怀疑正在查看 trustSrc(currentProject.url)
中的函数 trustSrc
的摘录不是控制器中定义.
您需要注入 $sce
服务在控制器和 trustAsResourceUrl
中的 url
那里.
在控制器中:
function AppCtrl($scope, $sce) {//...$scope.setProject = 函数 (id) {$scope.currentProject = $scope.projects[id];$scope.currentProjectUrl = $sce.trustAsResourceUrl($scope.currentProject.url);}}
在模板中:
I'm trying to set the src
attribute of an iframe from a variable and I can't get it to work...
The markup:
<div class="col-xs-12" ng-controller="AppCtrl">
<ul class="">
<li ng-repeat="project in projects">
<a ng-click="setProject(project.id)" href="">{{project.url}}</a>
</li>
</ul>
<iframe ng-src="{{trustSrc(currentProject.url)}}">
Something wrong...
</iframe>
</div>
controllers/app.js:
function AppCtrl ($scope) {
$scope.projects = {
1 : {
"id" : 1,
"name" : "Mela Sarkar",
"url" : "http://blabla.com",
"description" : "A professional portfolio site for McGill University professor Mela Sarkar."
},
2 : {
"id" : 2,
"name" : "Good Watching",
"url" : "http://goodwatching.com",
"description" : "Weekend experiment to help my mom decide what to watch."
}
};
$scope.setProject = function (id) {
$scope.currentProject = $scope.projects[id];
console.log( $scope.currentProject );
}
}
With this code, nothing gets inserted into the iframe's src
attribute. It's just blank.
Update 1:
I injected the $sce
dependancy into the AppCtrl and $sce.trustUrl() now works without throwing errors. However it returns TrustedValueHolderType
which I'm not sure how to use to insert an actual URL. The same type is returned whether I use $sce.trustUrl() inside the interpolation braces in the attribute src="{{trustUrl(currentProjectUrl))}}"
or if I do it inside the controller when setting the value of currentProjectUrl. I even tried it with both.
Update 2: I figured out how to return the url from the trustedUrlHolder using .toString() but when I do that, it throws the security warning when I try to pass it into the src attribute.
Update 3: It works if I use trustAsResourceUrl() in the controller and pass that to a variable used inside the ng-src attribute:
$scope.setProject = function (id) {
$scope.currentProject = $scope.projects[id];
$scope.currentProjectUrl = $sce.trustAsResourceUrl($scope.currentProject.url);
console.log( $scope.currentProject );
console.log( $scope.currentProjectUrl );
}
My problem seems to be solved by this, although I'm not quite sure why.
I suspect looking at the excerpt that the function trustSrc
from trustSrc(currentProject.url)
is not defined in the controller.
You need to inject the $sce
service in the controller and trustAsResourceUrl
the url
there.
In the controller:
function AppCtrl($scope, $sce) {
// ...
$scope.setProject = function (id) {
$scope.currentProject = $scope.projects[id];
$scope.currentProjectUrl = $sce.trustAsResourceUrl($scope.currentProject.url);
}
}
In the Template:
<iframe ng-src="{{currentProjectUrl}}"> <!--content--> </iframe>
这篇关于如何从 AngularJS 中的变量设置 iframe src 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!