如何使用AngularJS从远程URL获取模板 [英] How get a template from a remote URL with AngularJS

查看:195
本文介绍了如何使用AngularJS从远程URL获取模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用NW和AngularJS制作一个应用程序以制作桌面应用程序,我想要的是从服务器(html,css,js)获取文件.

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).

然后我要执行以下代码:

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: '/'
    });
});

问题是,当我运行应用程序时,它没有获取模板的html,那么我不确定该想法是否对AngularJs有效,或者我是否需要更改其逻辑以获取html的内容

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.

我遇到了错误

错误:$ sce:insecurl处理来自不受信任来源的资源 封锁

Error: $sce:insecurl Processing of a Resource from Untrusted Source Blocked

感谢您的帮助.

推荐答案

我在互联网上进行了一些搜索,发现了一个适合我的解决方案.

I was doing some search on internet and I found a solution that works for me.

这个想法是添加一个域,因为默认情况下angularJs仅支持相同的域,然后我们可以使用以下代码添加一个带有"$ sceDelegateProvider"的白名单

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

.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/**'
    ]);
 });

之后,当我们设置templateURL时,就可以使用远程服务器了.

after that when we will set the templateURL, we can use the remote server.

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

这篇关于如何使用AngularJS从远程URL获取模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆