I am working with an Angular factory that generates a function including a variable to select an array for a dropdown. While I have successfully set the variable from a user selection in a controller, I am facing difficulties getting this variable into the function at the controller.
The factory contains multiple arrays and a switch() function for selection based on input. The factory returns a function as shown in the following code snippet where ddSelections is one of the arrays:
languageFactories.factory('changePostDdFactory', ['$translate', function (translate) {
return {
withLangChoice: function (langKey) {
//variables containing arrays and switch() logic
return ddSelections;
}
}
}]);
The HTML code for the button dropdown responsible for displaying the selected array is as follows:
<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>
I am struggling with the controller for the button dropdown directive. While hardcoding some values works, it does not align with good Angular practices. When using variables, I encounter various issues. It seems like utilizing $scope may be the way to go, but there are uncertainties surrounding it. The function $scope.getCurrentLanguage appears to be causing problems. Here is the relevant part of the code:
residenceApp.controller('postButtonController2', ['$translate', '$scope', 'changePostDdFactory',
function ($translate, $scope, ddSelections) {
//$scope.getCurrentLanguage = 'en';
//$scope.ddMenuOptions = ddSelections.withLangChoice($scope.getCurrentLanguage);
$scope.getCurrentLanguage = function ($translate) {
alert('here I am'); //does not work
$translate.use();
return $translate.use();
};
$scope.ddMenuOptions = ddSelections.withLangChoice($scope.getCurrentLanguage);
$scope.ddMenuSelected = {};
$scope.$watch('ddMenuSelected', function (newVal) {
//do something when watch() triggers
}, true);