var app = angular.module('plunker', []);
app.component('child', {
template: `
<h2>Child</h2>
<p>Message: {{$ctrl.message}}</p>
`,
bindings: {
registerParent: '&'
},
controller: function($scope){
var ctrl = this;
ctrl.registerParent({childRef: ctrl});
ctrl.childFunction = function(message){
ctrl.message = 'Child function: ' + message;
};
}
});
app.component('parent', {
template: `
<h1>Parent</h1>
<button ng-click="$ctrl.childRef.childFunction(\'called from parent\')">Notify child</button>
<child register-parent="$ctrl.register(childRef)"></child>
`,
controller: function(){
var ctrl = this;
ctrl.childRef;
ctrl.register = function(child){
ctrl.childRef = child;
};
}
});
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<link rel="stylesheet" href="style.css" />
<script data-require="angular.js@1.5.x" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.11/angular.min.js" data-semver="1.5.11"></script>
<script src="app.js"></script>
</head>
<body>
<parent></parent>
</body>
</html>
/* Put your css in here */