0

In AngularJS, if a parent function wraps a child function, how are parameters in a child function available if the parent did not contain them?

In the example below, the parent function (green arrow) does not have any parameters supplied to it. Within the body is a child function (yellow arrow) that DOES have a parameter.

If the parent did not contain this parameter then how is it possible to pass it into the child function?

Confusing snippet

2
  • Its a dependency injection , its registering function then it parsing the arguments and giving what you need Commented Nov 17, 2014 at 14:05
  • Aaah... I was under the impression that the DI was only happening at the entry point. This would mean that Angular is resolving everything in the call stack before any execution is performed. Is that correct? Commented Nov 17, 2014 at 14:11

1 Answer 1

1

This is more of an understanding-of-the-language question, than an Angular question.

Short answer: The internals of AngularJS call the method that is at the controller key in the returned object. It just happens to call it with a parameter of $scope

Any arguments passed or not passed to a function do not necessarily have anything to do with arguments passed to functions that are created inside of the "parent" function's scope - even though a parent-child relationship is not the correct way of thinking about how these functions are related).

Maybe an example will help better:

var t = function() {
   return {
    controller: function(someArgument) { 
         console.log(someArgument); 
    },
    foo: function(bar) {
      console.log(bar);
    }
   }
}

var obj = t();
obj.controller('hello');
obj.foo('world!');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.