Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have an angular js controller that I am writing in a way so other controllers can inherit from it. Instead of defining the function as a single one in the angular's controller function I am writing it in the following way:

   function SomeCtrl($scope)
   {
       this.some_field = "1234";
       this.scope.externalMethod = angular.bind(this, this.externalMethod);
       this.scope.anotherMethod = angular.bind(this, this.anotherMethod);
       return this;
   }

   SomeCtrl.prototype.externalMethod = function()
   {
       //Do something
       //....(Don't worry about this method, its just to highlight that I can call this method from $scope.externalMethod)
   }

   //These are the methods of interest
   SomeCtrl.prototype.instanceMethodOne = function()
   {
       //Do something....
   }

   SomeCtrl.prototype.anotherMethod = function()
   {
       this.instanceMethodOne(); //---> Problem here!
       //Carry on
       //....
   }

   angular.module('some_module') //Previously defined
   .controller('SomeCtrl', SomeCtrl)

So the problem that I am having now is to have a reference (this) inside the method "anotherMethod", which calls a class instance method "instanceMethodOne". This resolves to null as the self reference "this" is not resolved at that point. Is there any way to reference an object inside its instance method like in this case?

share|improve this question
    
What is invoking 'anotherMethod'? –  sma Feb 22 at 11:59
    
anotherMethod should be invoked from $scope. In the case of unittest just from $scope itself ($scope.anotherMethod) and in the case of a view directly called from a button's press down event. –  Farhan Rahman Feb 22 at 12:00

1 Answer 1

up vote 0 down vote accepted

The problem has been resolved.

UPDATE AND FIXED:

Okay so I have looked into this problem. I should actually update the problem. It actually looks more like this:

   function SomeCtrl($scope)
   {
       this.some_field = "1234";
       this.scope.externalMethod = angular.bind(this, this.externalMethod);
       this.scope.anotherMethod = angular.bind(this, this.anotherMethod);
       return this;
   }

   SomeCtrl.prototype.externalMethod = function()
   {
       //Do something
       //....(Don't worry about this method, its just to highlight that I can call this method from $scope.externalMethod)
   }

   //These are the methods of interest
   SomeCtrl.prototype.instanceMethodOne = function()
   {
       //Do something....
   }

   SomeCtrl.prototype.anotherMethod = function()
   {
       var aCallBack = function()
       {
          //Some stuff...
          this.instanceMethodOne(); //---> Problem here!
       }

       //Carry on
       //....
   }

   angular.module('some_module') //Previously defined
   .controller('SomeCtrl', SomeCtrl)

So the fix was to first create a reference to the object's method. That way I could call it within the callback function. The "this" in the callback function was referring to that function itself. Sorry for false alarm...This is fixed now! Thanks @sma for looking into it.

share|improve this answer
    
It will be good if you update this post with answer rather than pointing to the question to find answer in it. –  Jenish Rabadiya Feb 22 at 13:24
1  
Thanks for the heads up Jenish. I have update the answer. –  Farhan Rahman Feb 22 at 13:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.