$ sce:在1.2.9版本上与本地Url一起使用

时间:2014-01-18 21:21:24

标签: angularjs escaping angularjs-ng-include

我有以下代码:

<span ng-include="getHeaderTemplate()"></span>

getHeaderTemplate()返回“templates / header / prospection.html”

然而,我收到以下错误:

Error: [$sce:insecurl] Blocked loading resource from url not allowed by $sceDelegate policy.  URL: templates/header/prospection.html
http://errors.angularjs.org/1.2.9/$sce/insecurl?p0=templates%2Fheader%2Fprospection.html
    at http://localhost/piquick/laravel/public/vendor/angular-1.2.9.js:78:12
    at Object.getTrusted (http://localhost/piquick/laravel/public/vendor/angular-1.2.9.js:12739:17)
    at Object.sceParseAsTrusted (http://localhost/piquick/laravel/public/vendor/angular-1.2.9.js:13168:22)
    at Scope.$digest (http://localhost/piquick/laravel/public/vendor/angular-1.2.9.js:11800:40)
    at Scope.$apply (http://localhost/piquick/laravel/public/vendor/angular-1.2.9.js:12061:24)
    at http://localhost/piquick/laravel/public/vendor/angular-1.2.9.js:1302:15
    at Object.invoke (http://localhost/piquick/laravel/public/vendor/angular-1.2.9.js:3710:17)
    at doBootstrap (http://localhost/piquick/laravel/public/vendor/angular-1.2.9.js:1300:14)
    at bootstrap (http://localhost/piquick/laravel/public/vendor/angular-1.2.9.js:1314:12)
    at angularInit (http://localhost/piquick/laravel/public/vendor/angular-1.2.9.js:1263:5) 

Url位于同一台服务器上(它是本地网址),所以我不明白为什么会收到此错误。

这是关于$ sce

的文档

http://docs.angularjs.org/api/ng.$sce#resourceurlpatternitem

2 个答案:

答案 0 :(得分:2)

看起来AngularJS的严格上下文转义(SCE)模式已阻止从“不安全”的URL加载资源。

他们的文档说:“浏览器的同源策略和跨域资源共享(CORS)策略适用,可能进一步限制模板是否成功加载。(例如,跨域请求既不适用于所有浏览器也不适用于所有浏览器file://某些浏览器上的请求)“

尝试使用以下调用包装模板网址:

$sce.trustAsUrl('templates/header/prospection.html')

Here's more info.

答案 1 :(得分:1)

为我工作相同的配置。你能告诉我们plnkr的例子吗?