问题描述
我该如何做这样的事情:$sce.trustAsResourceUrl('URL_HERE');
How can I do something like this:$sce.trustAsResourceUrl('URL_HERE');
全局,就像在主应用程序的 config()
或 run()
函数中一样,以便任何具有 URL_HERE
的 iFrame、img src 等会起作用吗?
Globally, like in the main app's config()
or run()
functions so that any iFrames, img src etc that have URL_HERE
will work?
文档在解释这一点方面相当糟糕.
Docs are rather poor at explaining this.
推荐答案
我刚刚从上一个答案中阅读了您的评论.不确定您是否找到了解决方案.似乎您正在寻找白名单类型的东西.我最近发现 $sce 有一个白名单功能.
I just read your comment from the previous answer. Not sure if you found a solution yet. Seems you are looking for a whitelist type of thing. I recently found this out that there's a whitelist function for $sce.
摘自 $sceDelegateProvider 的 AngularJS 文档:
angular.module('myApp', []).config(function($sceDelegateProvider) {
$sceDelegateProvider.resourceUrlWhitelist([
// Allow same origin resource loads.
'self',
// Allow loading from our assets domain. Notice the difference between * and **.
'http://srv*.assets.example.com/**']);
})
有了这个,你可以像这样在 iframe 中进行字符串插值:
With this you can do string interpolation in iframes like this:
<iframe ng-src="{{ 'http://srv1.assets.example.com/' + url_asset }}"></iframe>
这篇关于$sce.trustAsResourceUrl() 全局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!