0

Working on creating my first custom directive in AngularJS, but am having some problems when using the templateURL parameter; it doesn't actually request the page that I am attempting to call.

When I use the plain templateparameter, it works as expected though. I think that maybe my path being used in the templateURL may be incorrect??

This is my HTML in my main page:

<div class="container" ng-controller="formFields">
    <p>{{person.name}}</p>
    <div ng-sparkline person="person"></div>
</div>

Here is my custom directive:

myApp.directive('ngSparkline', function() {
  return {
    replace: true,
    restrict: 'EA',
    scope: {
        person: '='
    },
    //template: '<p class="lead">Hi, {{person.name}}!</p>'
      templateURL: '/js/directives/formFields.html'
  }
});

Here is my formFields.html file:

<p class="lead">Hey, {{person.name}}</p>


This is how my app directory is laid out: My directives.js file is located in the js directory, while the formFields.html file is located in /js/directives/...
directory layout

2 Answers 2

2

For one, you have miscased templateUrl as templateURL. If this does not fix your problem, it might be because of the absolute URL.

Are you serving the file via a server or did you open it in a browser via the disk?

If your link is file://C://www/html/js/directives, the absolute path /js/directives targets file://C:/js/directives, where no template code lies, obviously. In that case you need to use relative paths.

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

2 Comments

This application is hosted on a server, so I know that my filepath is correct, based on other similar constructions of relative paths in the application.
The problem was the spelling mistake! Feeling dumb
1

You've got a spelling(case) mistake. templateUrl, not templateURL

2 Comments

I edited my answer. You've just mis-spelled templateUrl
Thanks! Can't believe I missed that! Rookie move :)

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.