Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    New in Angular 1.4 - New bindToController Syntax

    John LindquistJohn Lindquist

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



    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson




    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.