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
Code is attached below:
Child Component:
(function (angular) {
'use strict';
childController.$inject = ['$scope'];
function childController($scope) {
//needs $emit here
$scope.$emit("topic-123", 'any message');
}
angular.module('childModuleName', [
]).component('childComponent', {
templateUrl: 'templateUrl',
controller: 'childController'
}).controller('childController', childController);
})(angular);
Parent Component:
(function (angular) {
'use strict';
controllerName.$inject = ['$scope'];
function controllerName($scope) {
var _this = this;
function toBeCalledOnEmit() {//some code}
var vm = {
toBeCalledOnEmit: toBeCalledOnEmit
}
$scope.$on('topic-123', function(event, msg) {
// @TODO
toBeCalledOnEmit();
});
angular.extend(_this, vm);
}
angular.module('moduleName', [
]).component('parentComponenet', {
templateUrl: 'templateUrl',
controller: 'controllerName'
}).controller('controllerName', controllerName);
})(angular);