2016-08-12 74 views
1

我正在使用NW和AngularJS創建桌面應用程序的應用程序,我想要的是從服務器(html,css,js)獲取文件。如何使用AngularJS從遠程URL獲取模板

然後我想要做的事,如下面的代碼:

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

的問題是,當我運行它是沒有得到模板的HTML應用程序的話,我不知道這種想法是在AngularJs上有效,或者如果我需要改變它的邏輯來獲取html的內容。

我收到錯誤

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

感謝您的幫助。

回答

0

我在互聯網上做了一些搜索,我找到了適合我的解決方案。

的想法是添加一個領域,因爲在默認情況下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/**' 
    ]); 
}); 

,當我們將設置templateURL,我們可以使用遠程服務器。

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

由於Cross-Origin Resource Sharing規則,您無法直接從遠程服務器加載內容。

一個比較簡單的解決方法是使用類似Nginx的代碼來使內容看起來像來自您自己的服務器。

如果您可以控制遠程服務器,則可以簡單地添加一個Access-Control-Allow-Origin標頭。