2014-09-03 2 views
0

드롭 다운의 배열을 선택하는 변수가있는 함수를 생성하는 각도 팩토리가 있습니다. 컨트롤러의 사용자 선택에서 변수를 설정해야하는 것 같습니다. 그 두 조각 작동하지만 컨트롤러에서 함수에 변수를 가져올 수 없습니다.각도 팩터 리에서 함수에 변수를 설정하십시오.

팩토리에는 여러 배열과 하나의 스위치를 선택하는 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> 

이다. "좋은 앵귤러 코드"처럼 보이지는 않지만 일부 코드는 작동합니다. 변수가 생길 때 나는 다양한 문제를 겪는다. 나는 $ 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); 

답변

1

아마도 함수를 호출해야합니까? 페이지로드에있는 공장 함수에 변수를 가져옵니다 $scope.ddMenuOptions = ddSelections.withLangChoice($scope.getCurrentLanguage($translate));
대신
$scope.ddMenuOptions = ddSelections.withLangChoice($scope.getCurrentLanguage);

+0


. 어떤 이유로 인해, $ translate.use()와 같이이 배열에서는 언어 변경이 작동하지 않습니다. $ translate에서 가져 오지는 않지만 다른 문제 일 수 있습니다. $ translate는 일반 HTML을 계속 변경합니다. 고마워, 한 걸음 앞으로. –