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:
(function (angular) {
'use strict';
controllerName.$inject = [];
function controllerName() {
var _this = this;
function toBeCalledOnEmit() {//some code}
var vm = {
toBeCalledOnEmit: toBeCalledOnEmit
}
angular.extend(_this, vm);
}
angular.module('moduleName', [
]).component('parentComponenet', {
templateUrl: 'templateUrl',
controller: 'controllerName'
}).controller('controllerName', controllerName);
})(angular);
child component:
(function (angular) {
'use strict';
childController.$inject = [];
function childController() {
//needs $emit here
}
angular.module('childModuleName', [
]).component('childComponent', {
templateUrl: 'templateUrl',
controller: 'childController'
}).controller('childController', childController);
})(angular);
I prefer working with a separate event service that exposes subscribe and notify functions. But if you prefer to emit from the child component then it would look like this:
Child Component
Parent Component
Code is attached below:
Child Component:
})(angular);
Parent Component:
})(angular);
You can do it like this using $rootScope. It works fine in my case -
child component:
Parent Component: