12

I am locally developing a single page app.

I cannot seem to load an HTML file as a template using a simple custom directive in Angularjs. If I attempt to use raw html using the template: attribute, it works just fine but I have over 400 lines of code i need to use in this template, and as soon as I try to use templateUrl: nothing loads. I have tried like 30 possible paths including copying the HTML file to many different locations such as C:/my-customer.html in a desperate attempt, here is my project structure:

    App--
        |
        css
        |
        img
        |
        js--
           |
           main.js
           |
           foundation.js
           |
           my-customer.html
        |
        templates--
                  |
                  my-customer.html
        |
        bcf.html
        |
        fluidType.html
        |
        start.html
        |
        my-customer.html

Keeping it simple I am using the example from the angular docs here is my code for the directive (inside main.js with my controller):

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

again I have tried paths like: ../my-customer.html ../templates/my-customer.html and so on but nothing is working for templateUrl:, I am on windows with no local web server running if this helps.

Here are some console errors but as a newbie not sure what they mean, I have a feeling I need to run a web server and parse through 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)

New to Angularjs. I have hit a wall in the project and I am completely stuck now.

6
  • Similar issue here, this may help: stackoverflow.com/questions/16887018/… Commented Dec 30, 2014 at 17:47
  • Tried using this article before posting didn't resolve my issue unfortunately. I just added the console errors though, any help is greatly appreciated. Commented Dec 30, 2014 at 17:49
  • 1
    You are getting a CORS error. That means the URL of your html and the URL of your webapp are two different things. Commented Dec 30, 2014 at 17:50
  • 3
    Ah yes, about the CORS error: you should run angular app on some server (for example nodejs server), not statically as it will cause CORS errors Commented Dec 30, 2014 at 17:53
  • Thanks, I am copying this project to a linux box and going to finish the project there. Commented Dec 30, 2014 at 17:55

1 Answer 1

14

It should already be relative to the root. So, this should work:

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

If that doesn't work, first make sure you can browse your template directory:

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

You should be able to get the template directly. Once you find the template, then just remove the host and port.

Sign up to request clarification or add additional context in comments.

7 Comments

you can also remove starting / I think
Did you try to browse for the template directly?
yea this loads file:///C:/Users/Dylan/Google%20Drive/PA/Dev/Grossing%20App/templates/my-customer.html
file? Are you running in a web server? You need to be running a web server.
Your local PC probably has a web server built in. You should investigate and run it through the server. Then, all will work as expected.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.