I am a little confused when I am new to Angular. Take the following controller function as an example
function hello($scope) {
$scope.name = '张三'
}
This is a function declaration, $scope should be a formal parameter, right? But changing $scope to another identifier such as s won't work. It can be seen that $scope is the actual parameter passed in when the hello function is called. But these three lines of code are function declarations, so why are actual parameters passed in?
I probably realized that this is not a normal function declaration. Maybe it has something to do with Angular’s controller function binding mechanism? What exactly does it look like?
This kind of obtaining dependencies through parameter passing is a major feature of AngularJS - one of the manifestations of dependency injection.
But why can we get dependencies by just declaring parameters?
AngularJS uses
$injector
to manage dependent query and loading, such asIf there is no explicit declaration,
$injector
the dependency relationship is inferred based on the parameter name. At this time, the order of the parameters is meaningless. In other words, we can also declare like this:Well, this thing is just a function declaration where it is written. Whether the name of the formal parameter is important is completely decided by the reader. The ECMAScript standard parser does not think it is important (I guess), but Angular took js and wrote a "js" parser. . . In this way, the name of the function parameter is important
http://note.sdo.com/u/635412003927148913/n/s6cBN~lcIHwG4M1-Y000LW