简体   繁体   English

如何使用AngularJS从远程URL获取模板

[英]How get a template from a remote URL with AngularJS

I am making an app with NW and AngularJS to make a desktop app, what I want is to get the files from a server(html, css, js). 我正在使用NW和AngularJS制作一个应用程序以制作一个桌面应用程序,我想要的是从服务器(html,css,js)获取文件。

then I want to do something like the following code: 然后我想做类似下面的代码:

aux.config(function ($routeProvider) {
    $routeProvider
        .when('/testInformation/', {
        templateUrl: 'https://serverName/test.html',
        controller: 'shipmentInformationController'
    }).otherwise({
        redirectTo: '/'
    });
});

The problem is that when I run the app it is not getting the html of the template, then I am not sure if this idea is valid on AngularJs or if I need change the logic of that to get the content of the html. 问题是,当我运行应用程序时,它没有获取模板的html,那么我不确定这个想法在AngularJs上是否有效,或者我是否需要更改其逻辑以获取html的内容。

I am getting the error 我收到错误

Error: $sce:insecurl Processing of a Resource from Untrusted Source Blocked 错误:$ sce:insecurl来自不受信任源的资源的处理被阻止

Thanks for any help. 谢谢你的帮助。

You can't directly load content from a remote server due to Cross-Origin Resource Sharing rules. 由于跨域资源共享规则,您不能直接从远程服务器加载内容。

One relatively straightforward workaround is to proxy the content using something like Nginx to make it look like it came from your own server. 一种相对简单的解决方法是使用Nginx之类的内容来代理内容,以使其看起来像来自您自己的服务器。

If you have control over the remote server, you could simply add an Access-Control-Allow-Origin header. 如果您可以控制远程服务器,则只需添加Access-Control-Allow-Origin标头。

I was doing some search on internet and I found a solution that works for me. 我在互联网上进行搜索,发现了一个适合我的解决方案。

The idea is add a domain because by default angularJs only support the same domain, then we can add a white list with the "$sceDelegateProvider" like the folowing code 这个想法是添加一个域,因为默认情况下angularJs仅支持相同的域,然后我们可以使用“ $ sceDelegateProvider”添加白名单,如下所示

.config(function ($sceDelegateProvider) {

    $sceDelegateProvider.resourceUrlWhitelist([
        // Allow same origin resource loads.
        'self',
        // Allow loading from our assets domain.  Notice the difference between * and **.
        'https://serverName.com/**'
    ]);
 });

after that when we will set the templateURL, we can use the remote server. 之后,当我们设置templateURL时,就可以使用远程服务器了。

.when('/test1/', {
        templateUrl: 'https://serverName.com/html/test1.html',
        controller: 'test1'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM