Communication between Angularjs Controllers

Source: Internet
Author: User

In Angularjs Development Experience summary, we mentioned that we need to divide the angular controller according to the business, so as to avoid the overwhelming and omnipotent God controller. We split the controller away, but sometimes we need to communicate in the controller, which is generally a relatively simple communication mechanism, telling the companion controller that something you care about has changed. What should we do? If you are a javascript programmer, you will naturally think of asynchronous callback and responsive communication-event mechanism (or message mechanism ). Yes, this is angularjs's solution to communication between controllers. the only method recommended is angular way. Angularjs provides a bubble and tunnel mechanism for us in scope. $ broadcast broadcasts events to all sub-controllers, while $ emit transmits event bubbles to the parent controller, $ on is the event registration function of angularjs. With this function, we can quickly solve communication between angularjs controllers in angularjs mode. The Code is as follows: View:

 
 
  1. <div ng-app="app" ng-controller="parentCtr"> 
  2.     <div ng-controller="childCtr1">name : 
  3.         <input ng-model="name" type="text" ng-change="change(name);" /> 
  4.     </div> 
  5.     <div ng-controller="childCtr2">Ctr1 name: 
  6.         <input ng-model="ctr1Name" /> 
  7.     </div> 
  8. </div> 
Controller:
 
 
  1. angular.module("app", []).controller("parentCtr", 
  2. function ($scope) { 
  3.     $scope.$on("Ctr1NameChange", 
  4.   
  5.     function (event, msg) { 
  6.         console.log("parent", msg); 
  7.         $scope.$broadcast("Ctr1NameChangeFromParrent", msg); 
  8.     }); 
  9. }).controller("childCtr1", function ($scope) { 
  10.     $scope.change = function (name) { 
  11.         console.log("childCtr1", name); 
  12.         $scope.$emit("Ctr1NameChange", name); 
  13.     }; 
  14. }).controller("childCtr2", function ($scope) { 
  15.     $scope.$on("Ctr1NameChangeFromParrent", 
  16.   
  17.     function (event, msg) { 
  18.         console.log("childCtr2", msg); 
  19.         $scope.ctr1Name = msg; 
  20.     }); 
  21. }); 
Here, the name change of childCtr1 will be passed to the parent controller in bubble mode, and the parent controller will encapsulate the event in broadcast to all sub-controllers, while childCtr2 registers the change event and changes itself. Note that the parent controller must change the event name during broadcast. Jsfiddle link: http://jsfiddle.net/whitewolf/5JBA7/15/

This article from the "wolf" blog, please be sure to keep this source http://whitewolfblog.blog.51cto.com/3973901/1179536

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.