In my AngularJS controller I have an event handler which receives an argument. 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. I also tried with $window
but I get undefined for the same. 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. On clicking Yes in the modal the event is handled
function deleteRequirement() {
console.log("In delete requirement")
}