将范围值设置为angularjs

时间:2015-10-17 10:03:30

标签: javascript angularjs

我还是JavaScript的新手,我想知道如何设置$ scope值等于另一个数组的值并在另一个函数中使用它。这就是我想要做的事情:

(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);
        };

    }

})(); 

我知道$scope.langI18n是错误的,但我想知道如何改进这一点,以便它确实有效。

在设置$scope.lang时,我想到了一个可能的解决方案,使用一个函数将索引与数组langI18n进行比较,然后将langues值设置为setlang,但我需要一些启动对此。

1 个答案:

答案 0 :(得分:1)

您可以在此处使用对象阵列。

    private async Task<String> GetStringFromURI(String URIString)
    {
        Uri resourceUri;
        if (!Uri.TryCreate(URIString.Trim(), UriKind.Absolute, out resourceUri))
        {
            return null;
        }
        string responseText;
        try
        {
            response = await httpClient.GetAsync(resourceUri);
            response.EnsureSuccessStatusCode();
            responseText = await response.Content.ReadAsStringAsync();
        }
        catch (Exception ex)
        {
            responseText = ex.ToString();
        }
        return responseText;
    }
相关问题