Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript AngularJS Basics (1.x) Getting Started with Angular Your First Angular Directive

Hi all, I can't seem to get my Angular directive to work in this one. I think I've typed in everything as per the video.

Google Chrome developer tools says "uncaught sytax error: unexpected token . in my hello-world.js line 2.

new to this forum so don't know if my exercise is listed. I'm on the getting started with angular video, I think the my first directive exercise...

4 Answers

It doesn't like the newline after the app initialization, see below. This should work!

angular.module('foobar', []).directive('customDirective', function() { console.log('This is a custom directive!'); })

Nathan Heffley
Nathan Heffley
19,878 Points

What? JavaScript doesn't care about whitespace. I did it with a newline right after the module initialization with no problems. Please don't teach people to write terribly formatted code like what you did. That is a nightmare, and squishing the code like that is for JavaScript minifiers, not people.

Hi Maddie,

I can't see your code, but the solution to the last challenge section is:

angular.module('foobar', [])
.directive('customDirective', function() {
  console.log('This is a custom directive!');
});

Sounds like you may have had a semicolon improperly placed or something along those lines?
Best, Cena

Sam Hall
PLUS
Sam Hall
Courses Plus Student 8,636 Points

In case it is helpful to anyone, I got the same error. It was nothing to do with whitespace. Rather, it was because I had ended the first line with a semi-colon:

//Gives error: "Uncaught SyntaxError: Unexpected token ."
angular.module("todoListApp");
.directive('helloWorld', function() {
  return {
    template : "This is the hello world directive."
  }
});

//Working code. No ; on first line!!
angular.module("todoListApp")
.directive('helloWorld', function() {
  return {
    template : "This is the hello world directive."
  }
});

Same thing for me. Do you know what that would cause the code to fail?

Sam Hall
PLUS
Sam Hall
Courses Plus Student 8,636 Points

Hi Bryan Woodard,

I'm not sure if this answers your question, but the semi-colon breaks it because of a couple of features of JavaScript:

  1. As mentioned by Nathan above, JavaScript ignores whitespace/line breaks.
  2. The semi-colon in JavaScript is used to separate statements.

Working through this example:

//We write the code with whitespace/line breaks to make it easier to read...
angular.module("todoListApp")
.directive('helloWorld', function() {
  return {
    template : "This is the hello world directive."
  }
});

//... but it is actually the same as this, with all whitespace removed. One long statement!:
angular.module("todoListApp").directive('helloWorld',function(){return{template:"This is the hello world directive."}});

// Putting an accidental semi-colon and the end of line one has the 
// unwanted effect of separating to two statements like this:
angular.module("todoListApp");
.directive('helloWorld',function(){return{template:"This is the hello world directive."}});

//which means the .directive function is not being invoked on any object. 

Chrome gives the syntax error "unexpected token ." because it does not expect a statement to start with a dot.