Controllers

John Lindquist
InstructorJohn Lindquist
Share this video with your friends

Social Share Links

Send Tweet
Published 11 years ago
Updated 6 years ago

Controllers are the 'glue' of an AngularJS application. They connect your markup with your data and act as a sort of switchboard.

[00:00] To add a controller to our document here, we'll say NG-controller, specify the name we'll call it when we create it in our JavaScript, and then specify the name we'll call it when we refer to it in our HTML. To create our controller, I'll create a script tag and say, "angular.module," which is where we'll store our controllers. We'll give it a name of "app," which needs to match up with the name we refer to it in our HTML.

[00:27] App here needs to match the app here. We'll pass in an empty array of dependencies which doesn't matter for now, and then say ".controller." Now we give it the name that we refer to it earlier, first control, and then we'll pass in a named function of first control, and we're naming this function to make debugging easier later on down the road. Lastly, for convenience, we'll say first equals this, just to handle JavaScript scoping issues later on down the road, and then we can say first.greeting equals first.

[01:01] Then we finally have our controller hooked up to our HTML. As you can see, the property greeting of first is hooked up to the greeting in our NG model.

egghead
egghead
~ 28 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today