1

Why doesn't my simple directive work?

I'm starting to learn Angularjs and I'm writing a simple directive that i can't seem to make work.

(function()
{
    "use strict";
    
    var as = angular.module('my.directives',[]);
    
    as.directive('HelloWorld', function(){
        return
        {
            restrict:'AEC',
            replace:false,
            templateUrl: "assets/angular/app/organisations/partials/helloWorldTemplate.html"
        };
    });
}());

This is my directive which when run says there is an error at the replace:false, line. When i remove this line, the line that replaces it then shows the same error.

SyntaxError: unexpected token: ':'[Learn More] directives.js:11:10

Everything i've tried so far doesn't seem to work.

Community
  • 1
  • 1
456ty
  • 11
  • 1
  • try removing whitespaces in `return {`. I think JS tries to open a block scope – Aleksey Solovey Apr 05 '18 at 15:26
  • The JavaScript engine is doing automatic semicolon insertion after the `return` statement. – georgeawg Apr 05 '18 at 16:08
  • I've put the return on one line and it now works. If someone wants to turn the comment into an answer I'll accept it. – 456ty Apr 06 '18 at 09:31
  • although now that I'm trying to add the Link function it comes up with unexpected token ':' , My code is `link:function(scope,elem,attrs) {scope.$watch('message',function(value){console.log('message changed');}); scope.clearMessage = function(){scope.message='';} elem.bind('mouseover',function(){elem.css('cursor','pointer'});};` – 456ty Apr 06 '18 at 12:20
  • If possible share entire directive code and also link function doesn't need any semicolon at the end. – Srigar Apr 09 '18 at 12:13
  • This is not an "exact duplicate" to the question you posted above @georgeawg. No doubt this is an ASI issue but to call this a duplicate to that question is not really accurate. No one asking this specific question is ever through a search on this site going to come up with a question relating to ASI. – John Apr 09 '18 at 12:22

0 Answers0