Steffen Brueckner
Steffen Brueckner

Reputation: 209

Angular Translate: $translate undefined in controller

I am using ng-show with a custom function. This function calls '$translate.use'-method inside the controller.

Funny enough, this works for the main-page but not for all other controllers although all of them use the same peace of code.

The console tells me that $translate is undefined.

Here's my peace of code for the index-page:

app.controller('LangCtrl', function ($scope, $translate) {

    $scope.isenglish = function () {
        if ($translate.use() == 'en_US') {
            return true;
        }
        return false;
    }........

and for another controller:

app.controller('selectFormController', ['$scope', '$http', 'storage', '$rootScope', function ($scope, $http, storage, $rootScope, $translate) {

........

  $scope.isenglish = function () {
        if ($translate.use() == 'en_US') {
            return true;
        }
        return false;
    }

I am really puzzled - is anybody able to help me out?

Thanks a lot!!! Steffen

Upvotes: 2

Views: 2711

Answers (2)

Pankaj Parkar
Pankaj Parkar

Reputation: 136164

Its working in LangCtrl controller because you directly injecting $translate dependency. But in your selectFormController controller you are using inline array annotation you need to add dependency in array as string(dependency name) first and then you could use it inside the controller function

In short you missed to add $translate dependency in your selectFormController controller.

Code

app.controller('selectFormController', ['$scope', '$http', 'storage', '$rootScope', '$translate', 
function ($scope, $http, storage, $rootScope, $translate) {

Upvotes: 3

Aurelio
Aurelio

Reputation: 25792

It seems that you're not really injecting $translate, check this out:

app.controller('selectFormController', ['$scope', '$http', 'storage', '$rootScope', '$translate', function ($scope, $http, storage, $rootScope, $translate)

Mind the $translate after $rootScope

Upvotes: 3

Related Questions