ドロップダウンの配列を選択する変数を持つ関数を生成する Angular ファクトリがあります。コントローラーでのユーザー選択からその変数を設定する必要があるようです。これらの 2 つの部分は機能しますが、変数をコントローラーの関数に入れることができません。
ファクトリには、いくつかの配列と、1 つを選択するための 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 で作業する必要があると思いますが、それは疑問の余地があります。$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);