通过文件协议访问角度模板(即在localhost上)

时间:2016-03-20 17:37:56

标签: javascript angularjs google-chrome

修改 AngularJS Error: Cross origin requests are only supported for protocol schemes: http, data, chrome-extension, https

有没有办法通过Chrome file协议制作单独的HTML文件(里面有角度模板)?

例如,如果您复制此问题的代码并在桌面上运行它,它在Firefox中可以正常工作,但在Chrome中无效。

由于安全原因,Chrome似乎限制了它,但可能会有一些或多或少的传统方式来传递它。

的index.html

<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.min.js"></script>
<script src="templates.js"></script>

<body ng-app="myApp">
  <wrapper>
      <p>foo</p>
  </wrapper>
</body>

templates.js

(function(angular) {
    'use strict';

angular.module('myApp', [])

.directive('wrapper', function() {
    return {
        restrict: 'E',
        transclude: true,
        templateUrl:
        './tpl-wrapper.html'
    };
});

})(window.angular);

TPL-wrapper.html

<div style="background-color: #ccc">
    <div ng-transclude></div>
</div>

火狐

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:1)

我的意思是,如果您真的希望可以下载Google Chrome扩展程序:

Allow-Control-Allow-Origin

然后我会确定它是自动启用的,所以你可以使用它..

注意

这仅供开发使用,不适用于生产......因为用户无法自动知道您正在提供文件://