Enter Your Email Address to Watch This Lesson

Your link to unlock this lesson will be sent to this email address.

Unlock this lesson and all 998 of the free egghead.io lessons, plus get Angular 1.x content delivered directly to your inbox!



Existing egghead members will not see this. Sign in.

Isolate Scope "&"

6:03 Angular 1.x lesson by

The "&" in your isolated scopes within your AngularJS application will allow you to invoke a method within the scope that your directive lives in.

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

The "&" in your isolated scopes within your AngularJS application will allow you to invoke a method within the scope that your directive lives in.

Avatar
glasson

hi, what does the "message" parameter in the html code refers to? Is it a dummy variable? Does it bind to the first part of the expression "message:value" contained in the js-file? If yes, does it mean that the use of the directive in html pre-supposes knowing the name of the parameters define in the template? Or can I use another name?

In reply to egghead.io
Avatar
Eric

+1 to the earlier question - I'm trying to call a function being passed into my directive, and am unable to get things to work quite correctly.

See also my SO question here http://stackoverflow.com/questions/23548746/angular-isolate-scope-attribute-behaviors

Is there documentation on this somewhere you can recommend? It's all very vague on the angular documentation site.

Avatar
Joel

Basically you can think of it in terms of dependency injection and a named parameter where dial({message:value}) is specifying the value of that parameter. The hash in the dial call needs to contain a message property, and that message property's value will be supplied.

Frankly, I think this is weird, and personally prefer to user a service I've created or = object binding to accomplish this wort of thing.

In reply to Eric
Avatar
mike connor

if found there was a problem with the controller until i listed '$scope' in the array that is the 2nd parameter of controller. Like this:

.controller('PhoneCtrl', ['$scope', function ($scope) {

$scope.callHome = function (msg) {
        alert(msg);
    };
}])

John Lindquist: The "&" sign allows you to invoke an expression, or call an expression, evaluate an expression, whatever, on the parent scope of whatever the directive is on the inside of. If we have our directive in here, we'll just create something called a phone and if we want to call something in the controller scope, we would use the "&" sign.

Basically, say, we have a function here called, "Call Home," let's say, called home. From our directive, we would just a create that phone, return object with a scope, because we're isolating the scope from the parent scope, and I think we called it "Dial" with an "&" sign.

Then, in our template, what we're going to do is create an up button, that we can click to call home, so div button, and then call home. That would say "ng-click," "Dial," cause that's what this is, has a mapping to each other. You say "Dial" here, which is going to invoke whatever you pass in to "Dial."

Then we'll pass, we'll say "Dial" equals, and we called it "Call Home," so "Dial = Call Home." From there, let's see, I missed something somewhere, app control phone, phone app, oh, that's an attribute, not an element. That's what I do when I write the HTML before I write the JavaScript. The valid isolate scope definition for directive phone.

See, that's what you get for letting me do these live, instead of polishing them up afterwards. All right. We're set here. I put the template inside of the scope on accident, status, stupidity. "Call Home" is going to invoke the controller scope of calling home. What that means is you can do things like having input in here. You can input just some sort of model, I'm able to save value, and then you can send something to that scope by passing in that value.

We're going to call this...I want to say message and value. I needed to use a special syntax where you pass an object into there, so that it can get invoked, the expression can get evaluated that way.

This value is going to go along as the payload. It's going to expect a message. So, in here, this needs to have a message now, and then, in here, this will need to have a message as well. Then we can show the message itself.

Now if I type something in here, put call home, you can see, you can send a message from the phone to the controller, and have basically, no relationship between the two. This Call Home is basically just an API to the controller, or things it can access or know about for the directive to communicate outside of itself.

Even if these scopes are isolated, because, again, if you were to do multiple phone directives, and the scopes are isolated, you'll see the bindings would mess up.

I can type in there, and in here, and here, too, and have the binding working inside of the directive, but they don't impact any of the binding or update inside of the controller because that doesn't happen until you invoke that expression that you're passing along using the inside.

Again, just to review here, we set up a method that we want to call on the scope here called, "Call Home." We set up the relationship by passing it into an attribute called "Dial," passing in a message. That "Dial" is defined here with an "&" sign.

That means that if we invoke "Dial," then pass along an object with a message as the name, and value as the value, then we can pass in whatever value and communicate with the controller from the directive. It's much different from the "=" sign or the "@" sign.

I'll do a review of all three to compare them, but this one is basically communicating back and forth without doing any sort of explicit binding like the "=" sign does. So that's the "&" sign.

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