I have an Angular factory that produces a function with a variable that selects an array for a dropdown. It seems that I should set that variable from a user selection in a controller. Those 2 pieces work, but I can't get the variable into the function at the controller.
The factory has several arrays and a switch() function to select one. The factory returns a function. Here's some code. ddSelections is an array.
languageFactories.factory('changePostDdFactory', ['$translate', function (translate) {
return {
withLangChoice: function (langKey) {
//variables containing arrays and a switch() to select based on langKey
return ddSelections;
}
}
}]);
The HTML for the button dropdown that displays the selected array is
<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>
The controller for that button dropdown directive is where I am floundering. When I hardcode some things it works, though it doesn't look like "good Angular code". When its variables, I get a variety of problems. I presume I should be working with $scope, but maybe that's open to question. $scope.getCurrentLanguage seems to be the problem. Here's code.
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);