Chooce video playback speed
speed:1

New in Angular 1.4 - New bindToController Syntax

InstructorJohn Lindquist

Share this video with your friends

Send Tweet

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

This lesson is a Community Resource

A Community Resource means that it’s free to access for all. The instructor of this lesson requested it to be open to the public.

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.