javascript

Can one controller call another?  #angularjs #reactjs

  • When you need to dispatch the event from child controller to scopes upwards you can use $scope.$emit.
  • So effectively, if a controller carries out an action, and has a new state, then, it will have to broadcast the state, and it is up to other controllers to listen to that broadcast and respond .
  • In above code controllers are showing their own value on Html and when you will click on text you will get values in console .
  • 1) For Parent- Child controller – Using $scope of parent controller to emit/broadcast events.
  • controller: function($scope) { this.publicMethodOnParentDirective = function() { // Do something } }

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.

@ng_real_ninja: Can one controller call another? #angularjs #reactjs

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

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

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.

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.

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.

javascript

You might also like More from author

Comments are closed, but trackbacks and pingbacks are open.