I have been working on a code snippet where I am trying to create a generic function. This function, when given the name of a function in my controller, should be run from a factory.
app.factory('myfactory', function () {
return {
create: function (name_of_function) {
angular.element(document.body).append('<button ng-click="'+name_of_function+'">trigger my function</button>')
return "";
}
};
});
The 'create' function is implemented like this:
I pass the name of my function so that when I click on the button, it will execute the function found in the controller.
function HelloCtrl($scope, myfactory) {
//name of my function
myfactory.create("myfunction()");
$scope.myfunction=function(){
alert("it works")
}
}
Can someone guide me on how to achieve this?