Explore Courses Blog Tutorials Interview Questions
+2 votes
in Web Technology by (47.6k points)

Is it possible to have one controller use another?

For example:

This HTML document simply prints a message delivered by the MessageCtrl controller in the messageCtrl.js file.

<html xmlns:ng="">


<meta charset="utf-8" />

<title>Inter Controller Communication</title>



<div ng:controller="MessageCtrl">



<!-- Angular Scripts →

<script src="" ng:autobind></script>

<script src="js/messageCtrl.js" type="text/javascript"></script>



The controller file contains the following code:

function MessageCtrl() {

this.message = function() {

return "The current date is: " + new Date().toString();



Which simply prints the current date;

If I were to add another controller, DateCtrl which handed the date in a specific format back to MessageCtrl, how would one go about doing this? The DI framework seems to be concerned with XmlHttpRequests and accessing services.

1 Answer

+2 votes
by (106k points)

Yes, there are many ways to communicate with controllers. The best way would be sharing service to see the code below:-

function FirstController(someDataService) {

function SecondController(someDataService) {


The second way is emitting an event on scope see the code below:-

function FirstController($scope) { 

$scope.$on('someEvent', function(event, args) {}); 


function SecondController($scope) {

$scope.$emit('someEvent', args); 


Browse Categories