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="http://angularjs.org/">
<head>
<meta charset="utf-8" />
<title>Inter Controller Communication</title>
</head>
<body>
<div ng:controller="MessageCtrl">
<p>{{message}}</p>
</div>
<!-- Angular Scripts -->
<script src="http://code.angularjs.org/angular-0.9.19.js" ng:autobind></script>
<script src="js/messageCtrl.js" type="text/javascript"></script>
</body>
</html>
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.
If you want to call one controller into another there are four methods available
Controller and its scope can get destroyed, but the $rootScope remains across the application, that's why we are taking $rootScope because $rootScope is parent of all scopes .
If you are performing communication from parent to child and even child wants to communicate with its siblings, you can use $broadcast
If you are performing communication from child to parent ,no siblings invovled then you can use $rootScope.$emit
HTML
Angularjs Code
In above code console of $emit 'childEmit' will not call inside child siblings and It will call inside only parent, where $broadcast get called inside siblings and parent as well.This is the place where performance come into a action.$emit is preferrable, if you are using child to parent communication because it skips some dirty checks.
Its one of the best method, If you want to do child parent communication where child wants to communicate with immediate parent then it would not need any kind $broadcast or $emit but if you want to do communication from parent to child then you have to use either service or $broadcast
For example HTML:-
Angularjs
Whenever you are using child to parent communication, Angularjs will search for a variable inside child, If it is not present inside then it will choose to see the values inside parent controller.
AngularJS supports the concepts of "Seperation of Concerns" using services architecture. Services are javascript functions and are responsible to do a specific tasks only.This makes them an individual entity which is maintainable and testable.Services used to inject using Dependency Injection mecahnism of Angularjs.
Angularjs code:
It will give output Hello Child World and Hello Parent World . According to Angular docs of services Singletons – Each component dependent on a service gets a reference to the single instance generated by the service factory.
This method gets scope() from the element by its Id / unique class.angular.element() method returns element and scope() gives $scope variable of another variable using $scope variable of one controller inside another is not a good practice.
HTML:-
Angularjs:-
In above code controllers are showing their own value on Html and when you will click on text you will get values in console accordingly.If you click on parent controllers span, browser will console value of child and viceversa.
Following is a
publish-subscribe
approach that is irrespective of Angular JS.Search Param Controller
Search Choices Controller
Event Manager
Global
Here is a one-page example of two controllers sharing service data:
Also here: https://gist.github.com/3595424
Actually using emit and broadcast is inefficient because the event bubbles up and down the scope hierarchy which can easily degrade into performance bottlement for a complex application.
I would suggest using a service. Here is how I recently implemented it in one of my projects - https://gist.github.com/3384419.
Basic idea - register a pub-sub/event bus as a service. Then inject that event bus where ever you need to subscribe or publish events/topics.
I also know of this way.
But I don't use it too much, because I don't like to use jQuery selectors in angular code.
In angular 1.5 this can be accomplished by doing the following:
This is the parent component. In this I have created a function that pushes another object into my
productForms
array - note - this is just my example, this function can be anything really.Now we can create another component that will make use of
require
:Here the child component is creating a reference to the parents component function
addNewForm
which can then be bound to the HTML and called like any other function.