New in Angular 1.4 - New bindToController Syntax

John Lindquist
InstructorJohn Lindquist
Share this video with your friends

Social Share Links

Send Tweet
Published 10 years ago
Updated 6 years ago

Angular 1.4 introduces a new syntax for bindToController. John demonstrates how to use it and what the new options are.

[00:00] When you set bindToController as true on your directive, if you pass a message into your scope, it's actually going to take that message and put it onto the controller as instance. Body, a body message, or, here, the body as and then the message coming in from the scope. You can see we have the message in our HTML. That's what's going to get passed in.

[00:24] In 1.4, if I swap this from the scope down to the bindToController, then I'll just type "true" here in the scope. This will actually achieve the exact same thing. If I just change my message to something like "New syntax," then you can see it changes to "New syntax" in the result.

[00:50] What this allows you to do is to change the scope as well to an object that can take a separate set of attributes and pass them in. You see I have a note here. I'll just go ahead and add a note in the HTML. Pass in "Hello." Then I can just show note inside of my template. When I save and refresh, you can see it says, "New syntax Hello."

[01:18] Honestly, I can't really think of a scenario where I'd use both the scope and bindToController as these objects, but I do like bindToController being an object that can take those different attributes because it expresses the intent of what I'm trying to do better.

[01:32] The only gotcha here is that if you don't include either the object or scope is true, then it's not going to assign those properties to the controller at all.

egghead
egghead

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