我有一個Angular工廠生成一個函數,其中一個變量爲下拉列表選擇一個數組。看來我應該從控制器中的用戶選擇中設置該變量。這兩部分工作,但我無法將變量放入控制器的函數中。在Angular工廠的函數中設置一個變量
工廠有幾個數組和一個switch()函數來選擇一個。工廠返回一個功能。這裏有一些代碼。 ddSelections是一個數組。
languageFactories.factory('changePostDdFactory', ['$translate', function (translate) {
return {
withLangChoice: function (langKey) {
//variables containing arrays and a switch() to select based on langKey
return ddSelections;
}
}
}]);
用於顯示選定陣列按鈕下拉的HTML是
<div id="postBox" class="floatingSection" data-ng-controller="postButtonController2">
<button id="postButton" dropdown-menu="ddMenuOptions" dropdown-model="ddMenuSelected" class="btn-menu">{{ 'POST' | translate }}</button>
</div>
該按鈕下拉指令控制器是我在哪裏掙扎。當我硬編碼一些有用的東西,雖然它看起來不像「好的Angular代碼」。當它的變量,我得到各種各樣的問題。我認爲我應該與$範圍合作,但也許這是值得商榷的。 $ scope.getCurrentLanguage似乎是問題所在。這是代碼。
residenceApp.controller('postButtonController2', ['$translate', '$scope', 'changePostDdFactory',
function ($translate, $scope, ddSelections) {
//hardcoded works at page load and shows my intention
//$scope.getCurrentLanguage = 'en'; //creates Scope and Model for getCurrentLanguage & ddMenuOptions
//$scope.ddMenuOptions = ddSelections.withLangChoice($scope.getCurrentLanguage); //shows correct array from factory
//here's my latest of many attempts with a user selected variable that is accessed via the $translate directive
//per Batarang there is no Scope and Model for getCurrentLanguage & ddMenuOptions
$scope.getCurrentLanguage = function ($translate) {
alert('here I am'); //does not fire
$translate.use(); //getter per http://stackoverflow.com/questions/20444578/get-current-language-with-angular-translate
return $translate.use(); //should return 'en' or 'es'
};
$scope.ddMenuOptions = ddSelections.withLangChoice($scope.getCurrentLanguage); //no dropdown, no array change
//$scope.ddMenuOptions = ddSelections.withLangChoice(getCurrentLanguage); //page does not load
$scope.ddMenuSelected = {};
$scope.$watch('ddMenuSelected', function (newVal) {
//if watch() triggers, do something
}, true);
是的,是可以獲得所需的變量在頁面加載出廠功能。出於某種原因,更改語言不適用於這些數組,就像$ translate.use();沒有從$ translate中獲得,但也許這是一個不同的問題。 $ translate繼續改變正常的HTML。謝謝,它向前邁進了一步。 – 2014-09-03 15:08:15