Using $emit To Pass A Function From Child To Parent In AngularJS
I want to send the result of a function to a parent controller from a child. Here is my file structure -submission.controller.js --calculate.controller.js In calculate.controller.
Solution 1:
Assuming the controllers are used in such a way that they are parent-child of each other, you have two issues with the code you have provided:
$scope.$emitexpects arguments next to event name so if you want to pass a function's result, you can have a seperate function and then call it from there like$scope.$emit('validForm', myFunctionCall())Also, you can't use
$scope.$onlike that since it doesn't return anything. If you want to access the values sent from$emit, you can do that inside the listener function (second argument) i.e. something like this:$scope.$on('validForm', function(event, argument1) { // argument1 is the same as return value of myFunctionCall() });
Solution 2:
'Emit' from child.
$scope.$emit('validForm', function () {
var checkForm = false;
if(files === true) {
checkForm = true;
}
return checkForm;
});
Now Receive.
$scope.showValidZip = function () {
$scope.$on('validForm'), function(event, data) {
if(data === true)
return false;
});
return true;
};
Post a Comment for "Using $emit To Pass A Function From Child To Parent In AngularJS"