How get a template from a remote URL with AngularJS

倖福魔咒の 提交于 2019-12-11 06:34:41

问题


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

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.

I am getting the error

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

Thanks for any help.


回答1:


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.

If you have control over the remote server, you could simply add an Access-Control-Allow-Origin header.




回答2:


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

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

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


来源:https://stackoverflow.com/questions/38921884/how-get-a-template-from-a-remote-url-with-angularjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!