Peter Kellner
Peter Kellner

Reputation: 15478

Simple AngularJS app with service does not work

I've got a very simple angular app that I can not figure out what is wrong with. The code is on plunkr here: http://plnkr.co/edit/QQkP2HB6VGv50KDdBPag?p=preview and generates the error: Uncaught Error: [$injector:modulerr] Failed to instantiate module myApp due to: Error: [$injector:unpr] Unknown provider: testService

The code is below

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>simple problem I can not figure out</title>

    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.js"></script>

    <script type="text/javascript">

        (function() {
            'use strict';

            var myAppModule = angular.module('myApp', []);

            myAppModule.service('testService', function (testService) {
            });

            myAppModule.config(['testService',
                function (testService) {
            }]);

        })();

        </script>

    </head>
    <body >
    <div ng-app="myApp">
        <div>
           myApp Here
        </div>
    </div>
    </body>
    </html>

Upvotes: 0

Views: 54

Answers (1)

Himen
Himen

Reputation: 1450

There are multi phase in angular bootstraps process. in config phase you can just inject provider. for example you can use this code:

        myAppModule.config(['testServiceProvider',
            function (testServiceProvider) {
            }]);

To get more information please check this link:

https://github.com/angular/angular.js/wiki/Understanding-Dependency-Injection#configuring-providers

Upvotes: 2

Related Questions