AngularJS Tutorial - Inject and invoke








The following code shows how to use Inject and invoke.

Example


<!DOCTYPE html>
<html  ng-app>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.js"></script>
<script type='text/javascript'>
<!--from   w  w w.  j  a  v  a 2  s  . c  o  m-->
angular.module('main.app', []).factory('MyService', ['$http', function ($http) {
    return new function () {
        this.GetName = function () {
            return "MyName";
        };
    };
}]);
angular.injector(['ng', 'main.app']).invoke(function (MyService) {
    console.log(MyService.GetName());
});
</script>
</head>
<body>
</body>
</html>

The code above is rendered as follows: