BUSH
BUSH

Reputation: 45

List all the functions inside angularjs controller

I have an angularjs controller, inside that I have method1 and method2 defined.

Now I need a way to fetch information of the defined method inside the angular controller.

I tried with the $scope variable It gives me access to the functions but what I would need here is to get all the methods defined as list(or something similar).

when I say $scope.getAllMethods it should give me information of method1 and method2 which is defined inside the controller.

Upvotes: 1

Views: 1103

Answers (1)

Brian
Brian

Reputation: 5049

How is this?

  1. Get all properties of $scope.
  2. Test if they are of function type.
  3. If they are, push to an array and stringify them.

    $scope.methods = [];
    $scope.getAllMethods = function() {
    
    console.log($scope);
    
    var props = Object.getOwnPropertyNames($scope);
    
    for (var i = 0; i < props.length; i++) {
      if (typeof($scope[props[i]]) === 'function') {
        $scope.methods.push({
          'name': props[i],
          'function': $scope[props[i]].toString()
        })
      }
    }
    

    }

Upvotes: 2

Related Questions