how do I get ng-include directive to work with a Content Delivery Network?

前端 未结 2 1799
后悔当初
后悔当初 2020-12-03 17:27

Is it possible to get ng-include directive to work with a CDN?

In this case:

2条回答
  •  佛祖请我去吃肉
    2020-12-03 18:17

    Yes, you are right - the problem is with cross-domain call.
    Official docs say:

    By default, the template URL is restricted to the same domain and protocol as the application document. This is done by calling $sce.getTrustedResourceUrl on it. To load templates from other domains or protocols you may either whitelist them or wrap them as trusted values. Refer to Angular's Strict Contextual Escaping.

    and

    Please note: The browser's Same Origin Policy and Cross-Origin Resource Sharing (CORS) policy apply in addition to this and may further restrict whether the template is successfully loaded. This means that without the right CORS policy, loading templates from a different domain won't work on all browsers.

    Example:

    angular.module('myApp', []).config(function($sceDelegateProvider) {
      $sceDelegateProvider.resourceUrlWhitelist([
        // Allow same origin resource loads.
        'self',
        // Allow loading from outer templates domain.
        'http://cdn.somewebsite.com/templates/**'
      ]); 
    });
    

    Helpful links:

    • ngInclude
    • $sce

    So your problem may be solved by appropriate angular settings, but not for all browsers.

提交回复
热议问题