A brief discussion on 4 ways of angular controller communication
This article will introduce to you the 4 ways of angular controller communication. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
[Related recommendation: "angular tutorial"]
First summarize the 4 ways of angular controller communication:
Scope inheritance.
Broadcast events through $scope.
Event emitter module.
Serve.
1. Scope inheritance
Subscopes can access variables and functions declared in their ancestor scopes.
<div ng-controller="Controller1"> <div ng-controller="Controller2"> this prints '42':{{answer}} </div> </div> m.controller('Controller1', function ($scope) { $scope.answer = 42; }); m.controller('Controller2', function ($scope) { console.log($scope.answer); });
2. Broadcast events through $scope
$emit call can bubble up the scope, $broadcast will propagate to descendant scopes, $on can be registered listener.
<div ng-controller="Controller1"> <div ng-controller="Controller2"> </div> </div> m.controller('Controller1', function ($scope) { $scope.$on('ping', function (){ console.log('pong'); }); $scope.$broadcast('broadcast'); }); m.controller('Controller2', function ($scope) { $scope.$emit('ping'); $scope.$on('broadcast', function (){ console.log('broadcast'); }); });
3. Event emitter module event-emitter
The event-emitter module works similar to the scope emitter. They have 3 key differences:
event-emitter is scope independent, so it is ideal to use it in services that do not have access to the scope.
The functions to be used are named .on(), .emit().
There is no corresponding $broadcast() function.
<script type="text/javascript" src="angular.js"></script> <script type="text/javascript" src="event-emitter.js"></script> <script type="text/javascript"> var app = angular.module('app', []); app.factory('userService', function ($timeout, $window) { var emitter = $window.emitter(); var user = {}; //模拟http错误 $timeout(function () { user.emit('error', 'Could not connect to server'); }, 1000); return user; }); app.factory('profileService', function (userService) { var ret = { user: userService, }; userService.on('error', function () { console.log('get error'); }); return ret; }); </script>
4. The most commonly used communication is service
Because the service is a singleton, changing the value of the service in any component will To affect other components, the usage is very simple. Just list the service as a dependency, as shown in the above code.
For more programming-related knowledge, please visit: Programming Teaching! !
The above is the detailed content of A brief discussion on 4 ways of angular controller communication. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Since Windows has become the gaming platform of choice, it's even more important to identify its gaming-oriented features. One of them is the ability to calibrate an Xbox One controller on Windows 11. With built-in manual calibration, you can get rid of drift, random movement, or performance issues and effectively align the X, Y, and Z axes. If the available options don't work, you can always use a third-party Xbox One controller calibration tool. Let’s find out! How do I calibrate my Xbox controller on Windows 11? Before proceeding, make sure you connect your controller to your computer and update your Xbox One controller's drivers. While you're at it, also install any available firmware updates. 1. Use Wind

Angular.js is a freely accessible JavaScript platform for creating dynamic applications. It allows you to express various aspects of your application quickly and clearly by extending the syntax of HTML as a template language. Angular.js provides a range of tools to help you write, update and test your code. Additionally, it provides many features such as routing and form management. This guide will discuss how to install Angular on Ubuntu24. First, you need to install Node.js. Node.js is a JavaScript running environment based on the ChromeV8 engine that allows you to run JavaScript code on the server side. To be in Ub

This article will give you an in-depth understanding of Angular's state manager NgRx and introduce how to use NgRx. I hope it will be helpful to you!

How to use monaco-editor in angular? The following article records the use of monaco-editor in angular that was used in a recent business. I hope it will be helpful to everyone!

Do you know Angular Universal? It can help the website provide better SEO support!

Learning Laravel from scratch: Detailed explanation of controller method invocation In the development of Laravel, controller is a very important concept. The controller serves as a bridge between the model and the view, responsible for processing requests from routes and returning corresponding data to the view for display. Methods in controllers can be called by routes. This article will introduce in detail how to write and call methods in controllers, and will provide specific code examples. First, we need to create a controller. You can use the Artisan command line tool to create

The Angular project is too large, how to split it reasonably? The following article will introduce to you how to reasonably split Angular projects. I hope it will be helpful to you!

With the rapid development of the Internet, front-end development technology is also constantly improving and iterating. PHP and Angular are two technologies widely used in front-end development. PHP is a server-side scripting language that can handle tasks such as processing forms, generating dynamic pages, and managing access permissions. Angular is a JavaScript framework that can be used to develop single-page applications and build componentized web applications. This article will introduce how to use PHP and Angular for front-end development, and how to combine them
