Filippo oretti
Filippo oretti

Reputation: 49893

Angularjs Provider $get method error at config()

I've just setted up a simple Provider that would retrieve a language json file from a web server and serve it to Angular in the config() method.

So i did the provider:

 .provider('language', function () {

    var languageWsURL
      , languageCode
      , languageAppName;

    return {
      'setLanguageWsURL' : function (url) {

        languageWsURL =  url;
      },
      'setLanguageAppName' : function (name) {

        languageAppName = name;
      },
      'setLanguageCode' : function (code) {

        languageCode = code;
      },
      '$get': ['$rootScope', '$window', '$http', function ($rootScope, $window, $http) {

        var loadLanguage = function () {

          if (languageWsURL && languageCode && languageAppName) {

            $http({
              'method':'GET',
              'url': languageWsURL,
              'params': {
                'application':languageAppName,
                'langCode':languageCode
              }
            }).success(function (data) {

              if (data.data && data.data.length > 0) {

                $rootScope.lang = data.data;
              }
            }).error(function (err) {

              $window.console.log('Error while retrieving app lang - languageProvider : ' + err);
            });
          } else {

            $window.console.error('Missing params to load language in languageProvider');
          }
        };

        return {
          'loadLanguage':loadLanguage
        };
      }]
    };
  });

and i created a little config() in index.js

.config(['languageProvider', function (languageProvider) {

    languageProvider.setLanguageWsURL('http://localhost:3000');
    languageProvider.setLanguageAppName('antani');
    languageProvider.setLanguageCode('en');
    languageProvider.loadLanguage(); //error here

  }]);

The problem is that it wont work, the languageProvider.loadLanguage() throws an error in console that is quite difficult to read:

http://errors.angularjs.org/1.2.23/$injector/modulerr?p0=app&p1=TypeError%3A%20undefined%20is%20not%20a%20function%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A8000%2Fassets%2Fjs%2Findex.js%3A15%3A21%0A%20%20%2

Any clue please?

Upvotes: 0

Views: 346

Answers (2)

MiTa
MiTa

Reputation: 1340

languageProvider does not have method loadLanguage, language has. As you cannot inject service to config(), your code should look something like this :

.config(['languageProvider', function (languageProvider) {
   languageProvider.setLanguageWsURL('http://localhost:3000');
   languageProvider.setLanguageAppName('antani');
   languageProvider.setLanguageCode('en');      

}])
.run(['language',function(language){
   language.loadLanguage();
}]);    

Upvotes: 1

Chandermani
Chandermani

Reputation: 42669

$get object is used to create the actual service. It is not available at config stage.

Define you loadLanguage function outside your $get implementation if you want to call it at config stage.

Upvotes: 1

Related Questions