12

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

我似乎无法使用 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

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

app.directive('myCustomer', function() {
  return {
    templateUrl: 'my-customer.html'
  };
});

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

这里有一些控制台错误,但作为一个新手不确定它们的含义,我觉得我需要运行一个 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)

Angularjs 的新手。我在项目中碰壁了,现在我完全被卡住了。

4

1 回答 1

14

它应该已经相对于根。所以,这应该工作:

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

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

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

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

于 2014-12-30T17:48:56.937 回答