javascript - 在 Angular 工厂的函数中设置变量

标签 javascript angularjs angular-translate

我有一个 Angular 工厂,它生成一个带有变量的函数,该变量为下拉列表选择一个数组。看来我应该从 Controller 中的用户选择中设置该变量。这两个部分可以工作,但我无法将变量放入 Controller 的函数中。

工厂有几个数组和一个 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>

该按钮下拉指令的 Controller 是我陷入困境的地方。当我对某些东西进行硬编码时,它可以工作,尽管它看起来不像“好的 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);

最佳答案

也许你需要调用你的函数?
$scope.ddMenuOptions = ddSelections.withLangChoice($scope.getCurrentLanguage($translate));
而不是
$scope.ddMenuOptions = ddSelections.withLangChoice($scope.getCurrentLanguage);

关于javascript - 在 Angular 工厂的函数中设置变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25634445/

相关文章:

网页包 : You may need an appropriate loader to handle this file type

angular - 检查angular2是否存在翻译键

javascript - 使用 fabric.js 修改 Canvas 而不删除现有内容

javascript - 如何在 vue3 应用程序(vscode)中配置 Stylelint 以在保存时进行 lint

javascript - 如何在 Ajax 调用中调用 Controller 外部的 Angularjs Controller 函数

angularjs - 在angularjs中单击更改下拉选择的值

javascript - 具有多个根和动态需求的 Webpack

javascript - 如何将值存储到外部文件

angularjs - 模态加载angularjs ui bootstrap后的调用函数

angularjs - 如何在 Angular 翻译中最好地组织翻译字符串?