0

Here is my directive

(function() {
    angular.module('commentsDirective', [])

    .directive('mngComments', mngComments)

    function mngComments() {
        return {
            restrict: 'AE',
            scope: {
                moment: '='
            },
            templateUrl: 'comments.html',
            replace: true,
            controller: 'CommentsController',
            controllerAs: 'vm'
        };
    };
})();

Here is my file structure:

enter image description here

And I get this error message:

enter image description here

I'm not understanding this. My html file is in the same directory as my directive so why can't it find it?

6
  • you need to specify the relative path like this ./comments.html Commented Dec 24, 2017 at 17:35
  • I've tried that - I got the same error message Commented Dec 24, 2017 at 17:42
  • Instead of having a separate function, will it work with the normal construction? .directive('mngComments', function() { }) etc... (and like Bogdan says, relative path too) Commented Dec 24, 2017 at 18:09
  • 1
    The path is probably relative to the HTML page and not the directive location. So whatever path you prepend to load the directive should be used in the directive to load the template. Commented Dec 24, 2017 at 18:18
  • directive script location is irrelevant. Browser works off of page relative location Commented Dec 24, 2017 at 18:40

1 Answer 1

2

Path to template should be relative to page’s path. So if directory comments is placed at root, you should specify templateUrl as /comments/comments.html.

If you want to use your module in unknown paths, you can use $templateCache to “import” your template to your module first. Then you can specify custom URL used while defining templateCache.

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

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.