I am using angular 1.5 component and need to call function in parent controller from when $emit in child component. How we can do this?
Example:
(fu
You can do it like this using $rootScope. It works fine in my case -
child component:
(function (angular) {
'use strict';
childController.$inject = ['$rootScope'];
function childController($rootScope) {
$rootScope.$emit('myEvent',$scope.data)
}
})(angular);
Parent Component:
(function (angular) {
'use strict';
controllerName.$inject = ['$rootScope'];
function controllerName($rootScope) {
var _this = this;
function toBeCalledOnEmit() {//some code}
var vm = {
toBeCalledOnEmit: toBeCalledOnEmit
}
$rootScope.$on('myEvent', function(event, msg) {
toBeCalledOnEmit();
});
angular.extend(_this, vm);
}
})(angular);