I want to build a site, which supports several languages. So I wanted to use AngularJS Controllers to controll the view. I have written some script, but I get the following error: TypeError: Cannot read property 'getLang' of undefined
Here is my AngularJS code:
var myApp = angular.module('myApp',[]);
myApp.service('languageService' , function() {
var language = 'en';
return {
getLang: function() {
return language;
},
setLang: function(ind) {
if (ind == 0) {
language = 'en';
} else if (ind == 1) {
language = 'ru';
}
}
};
})
myApp.controller('ChangeLangCtrl', ['$scope', function($scope, languageService) {
$scope.changeLang = function(ind) {
languageService.setLang(ind);
}
}])
myApp.controller('NaviCtrl', ['$scope', function($scope, languageService) {
var currentLang = languageService.getLang();
if (currentLang == 'en') {
$scope.menu = ['About Us', 'Service', 'Contacts'];
} else {
$scope.menu = ['О нас', 'Наши Услуги', 'Связь с нами']
}
}])
So what could be my mistake? I mean, I initialize the language variable with en at the beginning. So why is the getLang() function then undefined?