Reputation: 8484
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.
Upvotes: 1
Views: 395
Reputation: 11963
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'
};
});
Upvotes: 0
Reputation: 43
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;
});
});
Upvotes: 1
Reputation: 3520
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!
Upvotes: 0