The ability to reply to discussions is limited to PRO members. Want to join in the discussion? Click here to subscribe now.

An Alternative Approach to Controllers

An Alternative Approach to Controllers

2:49
AngularJS makes your HTML very declarative. However, seeing the functions invoked in your HTML may not offer any clues as to what controller defines said functions. This video offers an approach to solving this problem by returning the controller in the controller definition so that we can opt for something like `ng-click="SomeCtrl.doesSomething()"` instead of simply writing `ng-click="doesSomething()"`.
Watch this lesson now
Avatar
egghead.io

AngularJS makes your HTML very declarative. However, seeing the functions invoked in your HTML may not offer any clues as to what controller defines said functions. This video offers an approach to solving this problem by returning the controller in the controller definition so that we can opt for something like ng-click="SomeCtrl.doesSomething()" instead of simply writing ng-click="doesSomething()".

Avatar
glasson

Hi, Question: what does "this" refers to? Which object is bound to the function passed as second argument of app.controller(...)?

In reply to egghead.io
Avatar
Roman Ganchenko

This refers to an object of inner scope of the anonymous function inside of AppCtrl controller.

In reply to glasson
Avatar
Pavel

Controller functions are actually considered to be constructors. So this refers to the controller instance created by $injector.

In reply to glasson
Avatar
Dmitri

Isn't it the same as ControllerAs syntax?

Avatar
Joel

Isn't it the same as ControllerAs syntax?

Basically.

In reply to Dmitri
Avatar
Aviad

It's kinda like this:
app.controller('AppCtrl', function AppCtrl(){
AppCtrl.prototype.sayHi = function(){
alert('hi');
}
});

right?

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