Greg
Greg

Reputation: 1710

set scope value equal to array element in angularjs

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.

Upvotes: 1

Views: 1308

Answers (1)

softvar
softvar

Reputation: 18435

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"}
];

Upvotes: 1

Related Questions