1

I am trying to inject $scope into angular-translate directive. But it shows

angular.min.js:6 Uncaught Error: [$injector:modulerr] http://errors.angularjs.org/1.4.8/$injector/modulerr?p0=navBar&p1=Error%3A%…eb%20(http%3A%2F%2Flocalhost%3A8080%2Fsrc%2Fjs%2Fangular.min.js%3A41%3A249)

the above error is encountering. I want to use $scope value from controller as $translateProvider.preferredLanguage($scope.selectedLang);

app.config(function ($translateProvider, $scope){
    $translateProvider.useSanitizeValueStrategy(null);
    $translateProvider.translations('english', {
        'data': 'I am Ram'
    });
    $translateProvider.translations('telugu', {
        'data': ' \u0C28\u0C47\u0C28\u0C41 \u0C30\u0C3E\u0C2E\u0C4D'
    });
    $translateProvider.preferredLanguage($scope.selectedLang);
});
app.controller('langTranslate', function ($scope){
    $scope.totalLang = ['english', 'telugu'];
    $scope.lang = 'english';
    $scope.selectedLang = 'english';
    $scope.$watch(function(){
        $scope.selectedLang = $scope.lang;
    });
});

If I remove $scope and $translateProvider.preferredLanguage($scope.selectedLang); from app.config it works fine. But I have to use $scope value there. Please help me to resolve this issue.

Mr_Perfect
  • 8,254
  • 11
  • 35
  • 62

3 Answers3

1

maybe this question will help you understand what you need to do

How to inject a service into app.config in AngularJS

instead of app.config($translateProvider, $scope)

try it app.run($translateProvider, $rootScope)

    app.run(function ($translateProvider, $rootScope){
    $translateProvider.useSanitizeValueStrategy(null);
    $translateProvider.translations('english', {
        'data': 'I am Ram'
    });
    $translateProvider.translations('telugu', {
        'data': ' \u0C28\u0C47\u0C28\u0C41 \u0C30\u0C3E\u0C2E\u0C4D'
    });
    $translateProvider.preferredLanguage($rootScope.selectedLang);
});
app.controller('langTranslate', function ($scope, $rootScope){
    $scope.totalLang = ['english', 'telugu'];
    $scope.lang = 'english';
    $rootScope.selectedLang = 'english';
    $scope.$watch(function(){
        $rootScope.selectedLang = $scope.lang;
    });
});
Community
  • 1
  • 1
googleler
  • 43
  • 4
  • no. Getting this error `Uncaught Error: [$injector:unpr] http://errors.angularjs.org/1.4.8/$injector/unpr?p0=%24translateProviderProvider%20%3C-%20%24translateProvider` – Mr_Perfect Jul 29 '16 at 06:05
  • did you load the translate provider? did you include the translate provider to you angular.module? – googleler Jul 29 '16 at 06:09
  • Everything is done. If use a value, for example, `$translateProvider.preferredLanguage('telugu');` directly, it works fine. but I have to pass variable from the controller. So what should I do? – Mr_Perfect Jul 29 '16 at 06:15
  • try putting it on a $rootScope.variable – googleler Jul 29 '16 at 06:22
0

Please find the documentation for config here: https://docs.angularjs.org/guide/module

Inside config block you can only inject Providers.

In order to work with it, you can use $rootScope provider because $scope is module.

Hope it helps you!

Cheers!

Varit J Patel
  • 3,497
  • 1
  • 13
  • 21
0

You can not ask for instance during configuration phase - you can ask only for providers. for more information read this guide

app.config(function (MyFactory){
    console.log(MyFactory.test);
});
app.factory('MyFactory', function(){
    return {
      test: 'testing'
    };
});
Durgpal Singh
  • 11,481
  • 4
  • 37
  • 49