This lesson is for PRO members.

Unlock this lesson NOW!
Already subscribed? sign in

New in Angular 1.4 - New bindToController Syntax

1:43 Angular 1.x lesson by

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

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

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

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.

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.

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."

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.

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.

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?