For example, the following code. This is a simple application that declares a module and a controller:
Angular.module (' myApp ', []) . Factory (function() { return { function(msg) {alert (msg);} } ) . Controller (' Mycontroller ',function($scope, greeter) { function() { Greeter.greet ("hello!" ); }; });
When Angularjs instantiates this module, it looks for greeter and naturally passes the reference to it:
<DivNg-app= "MYAPP"> <DivNg-controller= "Mycontroller"> <ButtonNg-click= "SayHello ()">Hello</Button> </Div></Div>
Internally, the ANGULARJS process is as follows:
Use the injector to load the application var injector = Angular.injector ([' ng ', ' myApp ']);
Load $controller service via injector: var $controller = injector.get (' $controller ');
var scope = Injector.get (' $rootScope '). $new ();
Load the controller and pass in a scope, as ANGULARJS does at run time var mycontroller = $controller (' Mycontroller ', {$scope: scope})
The above code does not describe how to find greeter, but it does work properly because $injector will be responsible for finding and loading it for us.
Declares the process of a module and a controller ANGULARJS