0

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?

2 Answers 2

1

$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.

Sign up to request clarification or add additional context in comments.

Comments

1

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();
}]);    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.