5

New to angular and I've been having trouble with what should be a simple directive for the past hour or so. Would really appreciate some help!

I believe hello should appear, can't seem to get it to work?

test.html

   <html>
   <head lang="en">
     <title>Test</title>
   </head>
   <body>
    <div ng-app="myApp">
        <hello></hello>
    </div>


    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.14/angular.min.js"></script>
    <script type="text/javascript" src="main.js"></script>
    </body>
  </html>

main.js

    var myApp = angular.module('myApp', []);
    myApp.directive("hello", function() {
     return {
        restrict: 'E'
        templateUrl:"hello.html"
      };
    })`

hello.html

<p>Hello</p>
4
  • 1
    Replace templateUrl: with template: <p>Hello</p> - does it work then? Commented Jul 9, 2014 at 17:42
  • 1
    Sorry didn't specify, I got it to work with just template but I'm having trouble with templateUrl, which is what I want to use. The simple Hello is just a placeholder Commented Jul 9, 2014 at 17:45
  • 1
    Im guessing your URL is wrong...if you open the console and head to the network tab you should see the request for hello.html and it'll probably be red. Commented Jul 9, 2014 at 17:59
  • 1
    try this link stackoverflow.com/questions/29528922/… Commented Feb 11, 2016 at 11:18

8 Answers 8

15

If you are testing locally and using Google Chrome then this will not work because AngularJS is making an Ajax call to these html files, but Chrome blocks this with the error:

XMLHttpRequest cannot load file:///[filepath...] Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.

You can verify this by opening up your developer console and viewing the errors.

To get around this a couple ways:

  1. You can create a simple web server to run your code. If you have python you can just run the command (EDIT: from your folder containing index.html):

    python -m SimpleHTTPServer

  2. You can disable same origin policy in Chrome: https://stackoverflow.com/a/6083677/1100379

  3. Use an Chrome extension which may do this for you. I looked one up and this was the first result: Allow-Control-Allow-Origin

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

1 Comment

This was the case using angular inside an android webview.
9

Everything works fine, just make sure your syntax is correct. Do not miss comma's in JSON

myApp.directive("hello", function() {
  return {
    restrict: 'E',
                 ^
    templateUrl:"hello.html"
  };
})

Demo: http://plnkr.co/edit/Z6rjbsuqzmcD4gBem36c

1 Comment

The code might work properly on Plunker, but might result in cross origin issues if we are running it locally and not using a local server.
1

Actually we need to give the relative path for the templateUrl directive. Relative path which totally depends on component file in which you are giving path.

lets assume your file path may be like - app/hello.html

then your templateUrl path should be like - templateUrl:"./hello.html"

Comments

0

open dev tools in chrome go to network tab find reqest for hello.html compare reqested path with your path to hello.html on server. Move hello.html in proper place or update templeteUrl

Comments

0

Open your NodeJS command propmpt and install http-server using npm install http-server

http-server is a simple zero configuration command-line http server.

Once installed, just use http-server -o in your NodeJS command prompt.

Comments

0
  1. Download Python and add it to you path environment variable

  2. Create a cmd file to start python server in the directory where your index.html stays.

2 a) Create a new text document in the same root as your index.html

2 b) write - python -m SimpleHTTPServer 8080

2 c) Save as type 'All Files' and with some name like startServer .cmd

  1. Now the workaround for failing AJAX call has been done. Oen in browser 127.0.0.1:8080/index.html

Comments

0

You can import template in the script and use it as a "template", not "templateUrl":

import hello from 'hello.html';
var myApp = angular.module('myApp', []);
myApp.directive("hello", function() {
 return {
    restrict: 'E'
    template: hello
  };
})

Comments

0

You can use:

userManagementApp.directive("ngCreateUserForm", function () {
    return {
        templateUrl : '/template/create.html'
    };
});

and in Server side, you must serve youre template like static file

in Go: e.Static("/template", "/path/to/static/folder") or

in httpd config:

Alias /template/ "/path/to/template/folder"

Comments

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.