1
我還是一個JavaScript新手,想知道如何設置一個$範圍值等於另一個數組的值,並在另一個函數中使用它。這就是我想要做的事:設置範圍值等於angularjs中的數組元素
(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
但我需要這方面的一些反衝啓動。
這看起來很有前途,但我在$ scope.lang [0]上得到一個錯誤:無法讀取未定義的屬性'0'。 – Greg
如果你從上面的答案中複製粘貼,那麼它應該是$ scope.langs(在結尾處有's') – Guillaume
好吧,現在這可以工作,但是我在$ scope.selectLang = $ scope.lang中遇到了以下問題[0]它返回2個對象。我怎麼能說只需要第一個或最後一個對象? – Greg