角度指令的Karma测试给出错误:$ sce:insecurl

时间:2016-09-27 14:35:42

标签: javascript angularjs karma-jasmine

我正在为一个名为日历周的指令编写测试,我收到以下角度误差:https://docs.angularjs.org/error/ $ sce / insecurl?p0 = http:%2F%2Fhere.com%2Fviews%2Fdirectives%2FcalendarWeek。 HTML

我的规格

describe('Tests for CalendarWeek Directive', function(){
  var el, scope, controller;

  beforeEach(function() {
    module('MyApp');
    return inject(function($injector, $compile, $rootScope, $httpBackend, $sce) {
      el = angular.element("<div calendar-week></div>");
      $compile(el)($rootScope.$new());
      $rootScope.$digest();
      controller = el.controller("calendarWeek");
      $sce.trustAsResourceUrl("http://here.com/views/directives/calendarWeek.html")
      $httpBackend.whenGET("http://here.com/views/directives/calendarWeek.html").respond({ hello: 'World' });

我有什么遗失的吗?

1 个答案:

答案 0 :(得分:0)

来自Angular docs: 默认情况下,Angular仅从与应用程序文档相同的域和协议加载模板。这是通过在模板URL上调用$ sce.getTrustedResourceUrl来完成的。要从其他域和/或协议加载模板,您可以将它们列入白名单或将其包装到受信任的值中。

您正尝试从其他域或其他协议加载资源。我认为您需要像这样包装您的网址:

  $httpBackend.whenGET($sce.trustAsResourceUrl("http://here.com/views/directives/calendarWeek.html")).respond({ hello: 'World' });

或者像这样将其列入白名单:

angular.module('myApp', []).config(function($sceDelegateProvider) {
  $sceDelegateProvider.resourceUrlWhitelist([
    // Allow same origin resource loads.
    'self',
    // Allow loading from our assets domain.  
    'http://here.com/views/directives/calendarWeek.html'
  ]);

});