1

I'm still new to JavaScript an was wondering how i can set a $scope value equals to a value of another array and use it in another function. This is what i'm trying to do:

(function () {
    'use strict';

    angular
        .module('app')
        .controller('LanguageController', LanguageController);

    LanguageController.$inject = ['$scope','$translate'];
    function LanguageController($scope,$translate) {


        $scope.lang = [
            "Nederlands",
            "Français",
            "English",
            "Deutsch",
            "Español"
        ];
        $scope.selectLang = $scope.lang[0];
        $scope.changeLang = function(){
            console.log("Lang === " + $scope.selectLang);
        }

        // When value of $scope lang change i would want do 
        //Change the value of this 
        $scope.langI18n = [
            "nl_NL",
            "fr_FR",
            "en_US",
            "de_DE",
            "es_ES"
        ];

        //And then use it here 
        $scope.setLang = function(langKey) {
            // You can change the language during runtime
            console.log("change to " +langKey )
            $translate.use(langKey);
        };

    }

})(); 

i know the $scope.langI18n is wrong but i'm wondering how i could improve this so it does work.

I thought of a possible solution when $scope.lang is set use a function that compares the index to the array langI18n and then set the langues value into setlang but i need some kick start on this.

1 Answer 1

1

You can use Array of Objects here.

$scope.langs = [
    {"nl_NL": "Nederlands"},
    {"fr_FR": "Français"},
    {"en_US": "English"},
    {"de_DE": "Deutsch"},
    {"es_ES": "Español"}
];
Sign up to request clarification or add additional context in comments.

3 Comments

This looks promising but i get an error on $scope.lang[0] :Cannot read property '0' of undefined.
if you copy pasted from the answer above, then it should be $scope.langs (with an 's' at the end)
Ok so now this works but i got the following problem on $scope.selectLang = $scope.lang[0] it returns 2 object. How can i say that in only need the first or last object ?

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.