未找到Angularjs templateUrl文件位置

Dyl*_*anH 12 angularjs angularjs-directive angular-template

我在本地开发单页应用程序.

我似乎无法使用Angularjs中的简单自定义指令将HTML文件作为模板加载.如果我尝试使用该template:属性的原始html ,它工作得很好但我有超过400行代码我需要在这个模板中使用,并且一旦我尝试使用templateUrl:任何负载.我尝试了30种可能的路径,包括将HTML文件复制到许多不同的位置,例如C:/my-customer.html,这是我的项目结构:

    App--
        |
        css
        |
        img
        |
        js--
           |
           main.js
           |
           foundation.js
           |
           my-customer.html
        |
        templates--
                  |
                  my-customer.html
        |
        bcf.html
        |
        fluidType.html
        |
        start.html
        |
        my-customer.html
Run Code Online (Sandbox Code Playgroud)

保持简单我使用角文档中的示例这里是我的指令代码(在我的控制器的main.js内):

app.directive('myCustomer', function() {
  return {
    templateUrl: 'my-customer.html'
  };
});
Run Code Online (Sandbox Code Playgroud)

再次,我尝试过路径,如:../my-customer.html ../templates/my-customer.html等等,但没有任何工作templateUrl:,我在Windows上没有本地Web服务器运行,如果这有帮助.

这是一些控制台错误,但作为一个新手不确定他们的意思,我有一种感觉,我需要运行一个Web服务器并解析通过http.

 XMLHttpRequest cannot load file:///C:/Users/Dylan/Google%20Drive/PA/Dev/Grossing%20App/templates/my-customer.html. Cross origin requests are only supported for protocol schemes: http, data, chrome-extension, https, chrome-extension-resource.
angular.js:11594 Error: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'file:///C:/Users/Dylan/Google%20Drive/PA/Dev/Grossing%20App/templates/my-customer.html'.
    at Error (native)
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js:87:261
    at m (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js:83:133)
    at f (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js:80:366)
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js:112:276
    at l.$eval (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js:126:84)
    at l.$digest (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js:123:195)
    at l.$apply (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js:126:362)
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js:17:448
    at Object.e [as invoke] (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js:37:96)angular.js:11594 (anonymous function)
angular.js:11594 Error: [$compile:tpload] http://errors.angularjs.org/1.3.8/$compile/tpload?p0=templates%2Fmy-customer.html
    at Error (native)
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js:6:416
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js:137:65
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js:112:276
    at l.$eval (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js:126:84)
    at l.$digest (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js:123:195)
    at l.$apply (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js:126:362)
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js:17:448
    at Object.e [as invoke] (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js:37:96)
    at d (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js:17:369)
Run Code Online (Sandbox Code Playgroud)

Angularjs新手.我在项目中遇到了障碍,现在我完全被困住了.

Dav*_*yon 13

它应该已经相对于root了.所以,这应该工作:

app.directive('myCustomer', function() {
  return {
    templateUrl: '/templates/my-customer.html'
  };
});
Run Code Online (Sandbox Code Playgroud)

如果这不起作用,请首先确保您可以浏览模板目录:

http://localhost/templates/my-customer.html

您应该能够直接获取模板.找到模板后,只需删除主机和端口即可.

  • 您的本地PC可能内置了Web服务器.您应该通过服务器调查并运行它.然后,所有都将按预期工作. (2认同)