0

From eggheadio lesson 22, I see a controller returning itself. Why does it have to return this? I thought just assigning scope's property to indicative name of the controller itself would do the trick.

var app = angular.module("phoneApp",[]);
app.controller("AppCtrl", function($scope){
     this.sayHi =  function(){ alert("hi");}
     $scope.AppCtrl = this;
    //return $scope.AppCtrl = this; //why this one when above line also works
})

and in html

<body ng-app="phoneApp">
  <div ng-controller="AppCtrl">
     <button ng-click="AppCtrl.sayHi()"></button>
  </div>
</body>

1 Answers1

1

Controllers are constructors and "return this" is implied/optional. So it isn't needed.

David Bennett
  • 1,885
  • 1
  • 12
  • 13
  • Thanks David, I also found out the way constructor function is exactly called in other SO post here that explains why this is returned. http://stackoverflow.com/questions/1978049/what-values-can-a-constructor-return-to-avoid-returning-this – user2633328 Oct 02 '13 at 01:32