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

Just one more step!

Check your inbox for an email from us and click link to unlock your lesson.



Defining a Method on the Scope

2:16 Angular 1.x lesson by

AngularJS 1.3: Controllers can no longer be global functions.

As of AngularJS 1.3, controllers can no longer reside on window as a globally accessible function. Controllers must be declared as part of an Angular module. For more details click here

Armed with Controllers and service factories, you'll probably want to do some work on the data to give your app behaviors. In this video we will look at providing that behavior by defining a method on the scope. This method will be bound within the HTML and update live as input changes.

Get the Code Now
click to level up

AngularJS 1.3: Controllers can no longer be global functions.

As of AngularJS 1.3, controllers can no longer reside on window as a globally accessible function. Controllers must be declared as part of an Angular module. For more details click here

egghead.io comment guidelines

Avatar
egghead.io

Armed with Controllers and service factories, you'll probably want to do some work on the data to give your app behaviors. In this video we will look at providing that behavior by defining a method on the scope. This method will be bound within the HTML and update live as input changes.

Avatar
Sultan

Hi,
Are there any plans to record these videos again so that they match the code examples? I recommend Egghead.io to many people (especially newbies) but when they see the videos are outdated it doesn't look good.

In reply to egghead.io
Avatar
Kostiantyn

Hi,
Are there any plans to record these videos again so that they match the code examples? I recommend Egghead.io to many people (especially newbies) but when they see the videos are outdated it doesn't look good.

100% agree, video should be updated

In reply to Sultan
Avatar
Joel

We update the code example and add annotations. We won't be updating the video for this lesson, preferring to work on new content.

It is greatly appreciated when comments are added that explain the specific issues in lessons when discovered to let others know what's up. There's only two of us here at egghead, and we do what we can.

Avatar
roberto

Hi Joel,

I'm also starting to realize the out-of-date videos as well- because I am not sure how to quite follow along properly.

Would you recommend to still watch these videos to get a basic gist of this and hopefully the intermediate vids clear up the confusion ?

Any advice on how to proceed would greatly be appreciated ! :)

In reply to Joel
Avatar
Rodrigo

I want to adhere to the requests above. I'm newbie to this, and it was sort of confusing when watching these videos, which are part of the "New To Angular? Start Learning Here?"

I've paid for this service, it would be nice that at least you could be taking care of the newbies. Experienced people could automatically realize of an outdated example and just go on, or even understand why it is outdated. For Newbies, it's just confusing and holds back the advance on the learning process.

In reply to Joel
Avatar
Paul

There is a jump from the revamped previous videos introducing states and services and this, which uses a factory. Very confusing.

The very principle of your training paradigm, "simple step by step tutorials" is broken here.

John Lindquist: Defining a method on your controller or on the scope in your controller is actually pretty straightforward. We'll just define a method called "reversed message," and this will be the function that returns scope data message. I'll split it up and reverse it, and then join it back together.

Then all we'll do in here is instead of saying, "data message," we'll invoke our method and say, "reversed message." We should be set up, so this is reversed. Now you can see it says, "This is reversed." That's really all you need to do, is just to find a method on your scope, give it a function that does something.

What I want to show you here that I see a lot of people do, is as much as possible, you don't want to reference the scope inside of a function, because we can really extract this out and pass in the message into our reversed message. We'll say, "We will pass in the data message," and then this can be just be "message," because we can pass in our message here.

That removes the dependency of the scope inside of our function, and makes it easier to test and all that sort of stuff.

You can see it works just the same as it always did. But now instead of having this scope dependency inside of your method here, you're just passing in the value into your method. So, much cleaner, a much better way of approaching that. Again, just message, message, and it returns our value.

It's just going to update that and do that check any time it sees the scope change, so that should be often enough for things like binding. If one of the scope models changes, it will revoke this reverse message function as well. So, there you go.

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