简体   繁体   English

当我将其名称作为字符串时如何执行Angular控制器方法

[英]How to execute a Angular controller method when I have its name as a string

In my AngularJS controller I have an event handler which receives an argument. 在我的AngularJS控制器中,我有一个事件处理程序,它接收一个参数。 The argument has a method name as string, which I need to execute. 该参数有一个方法名称为字符串,我需要执行它。 How do I call the method in my controller? 如何在控制器中调用该方法?

I have seen this question & answer but it doesn't work for me on the controller. 我已经看到了这个问题和答案,但是在控制器上对我不起作用。 Here is my code 这是我的代码

The event is created by a directive which is a confirmation dialog. 该事件由一个指令创建,该指令是一个确认对话框。 Event fires when the user clicks Yes. 用户单击“是”时将触发事件。

app.directive('yesNoModal', function() {
  return {
    restrict: 'E',
    scope: {
        yesNoData: '=',
    },
    controller: function($scope) {
        $scope.handleYes = function() {
            $scope.$emit('clickedYes', { "yesNoData": $scope.yesNoData  });
        }
        $scope.handleNo = function() {
            $scope.$emit('clickedNo');
        }
    },
    templateUrl: 'partials/templates/yes-no-modal.html',
  }
})

Event handler 事件处理程序

$scope.$on('clickedYes', function(event, arg) {
    console.log(arg)
    window[arg.yesNoData.yesMethod]()
});

window[arg.yesNoData.yesMethod] returns undefined in my case. 在我的情况下, window[arg.yesNoData.yesMethod]返回undefined I also tried with $window but I get undefined for the same. 我也尝试了$window但我没有得到相同的定义。 The function with the name of value of arg.yesNoData.yesMethod variable is defined in the controller as a method and the value of yesNoData is set before the modal is loaded. 在控制器中将名称为arg.yesNoData.yesMethod变量的值的arg.yesNoData.yesMethod定义为方法,并在加载模式之前设置yesNoData的值。 On clicking Yes in the modal the event is handled 在模式中单击“是”时,将处理事件

function deleteRequirement() {
    console.log("In delete requirement")
}

使用$scope[arg.yesNoData.yesMethod]()代替window[arg.yesNoData.yesMethod]()因为它是$scope对象而不是window对象中的方法

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我将其名称作为字符串时如何执行 JavaScript function - How to execute a JavaScript function when I have its name as a string 如何执行 JavaScript Function 当我将其名称作为字符串时(使用具有重载参数的参数) - How to execute a JavaScript Function When I have its name as a string (with parameters that have an overload argument) 当我将其名称作为字符串时执行 JavaScript 函数 - 不工作 - Execute a JavaScript function when I have its name as a string - Not working 当我将其名称作为字符串时,如何执行私有JavaScript函数 - How to execute a private JavaScript function when I have its name as a string 当我不带参数动态将其名称作为字符串发送时,如何执行javascript函数 - how to execute javascript function when i send its name as string dynamically with out parameters 当其名称作为字符串传递给函数时,如何执行JavaScript函数? - How to execute a JavaScript function when its name is passed as a string in function? 当我有一个带有对象名称的字符串时,如何调用对象的方法? - How do I call a method of an object when I have a string with the name of the object? 如果方法名称为字符串,如何调用方法 - How to call method if I have method name as string 如何将表单控制器名称及其值传递给 Angular 10 中的函数? - How to pass Form controller name and its value to a function in Angular 10? 如何在Edge中执行名称为字符串的JavaScript函数 - How to execute a JavaScript function having its name as a string in Edge
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM