Search code examples
javascriptangularjscoffeescriptangularjs-service

Angular - Unknown Provider from Provider


I have the strange issue, that somehow my own provider is not injecting correctly into my app.

This is my provider:

    angular.module '1425App'
    .provider 'OData',[() ->
      @_baseUrl = ''
      return {
        setBaseUrl: (value) ->
          @_baseUrl = value
          return
        $get: ['$http', '$q', ($http, $q) ->
          return {
          getAll: (resource) ->
            dfd = $q.defer()
            $http.get("#{@_baseUrl}/#{resource}").success (res) ->
              console.log res
              dfd.resolve()
              return
            return dfd.promise
          }
        ]
      }
    ]

This is my app + config block:

angular.module('1425App', [
  'ngCookies',
  'ngResource',
  'ngSanitize',
  'ui.router',
  'angular-loading-bar',
  'ngAnimate',
  'toaster',
  'ui.gravatar',
  'ngFitText',
  'google-maps',
  'mm.foundation',
  'restangular',
  'ui.select2',
  'ngTable',
  'ngGrid',
  'ngCsv',
  'ui.date',
  'ngDragDrop',
  'ui.sortable'
])
.config ($stateProvider, $urlRouterProvider, $locationProvider, $httpProvider, cfpLoadingBarProvider, baseUrl, ODataProvider) ->
  $httpProvider.interceptors.push('httpInterceptor')

  ODataProvider.setBaseUrl(baseUrl + '/odata/')
  cfpLoadingBarProvider.includeSpinner = false
...

Im getting following error:

Uncaught Error: [$injector:modulerr] Failed to instantiate module 1425App due to: Error: [$injector:unpr] Unknown provider: ODataProvider

This leads to my believe, that its an issue with injecting the provider into my app. Any idea what im missing?


Solution

  • Looking at you pasted snippet issue could be that you have config block appearing before oData provider has been registered. Try setting up the config block after the oDataProvider registration.

    Separate out config block from app registration and load it after your provider(s) have been registered. You can only configure the providers that are registered before the specific config block that uses it. This is not the case with constant though you can have them registered in any order.

    The above information (which was a bug) is as of 1.2.* version of angular, with 1.3 you can register providers even after the config block.