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.

3 Answers 3

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;
    });
});
Sign up to request clarification or add additional context in comments.

4 Comments

no. Getting this error Uncaught Error: [$injector:unpr] http://errors.angularjs.org/1.4.8/$injector/unpr?p0=%24translateProviderProvider%20%3C-%20%24translateProvider
did you load the translate provider? did you include the translate provider to you angular.module?
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?
try putting it on a $rootScope.variable
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!

Comments

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'
    };
});

2 Comments

you can use it via factory.. see this example stackoverflow.com/questions/16828287/…
here you can access a variable in config.

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.