Passing data between two controllers using $scope

Trying my best to explain a complex situation here. Please bear with me.

I'm currently working on an AngularJS app and struggling to update the UI ($scope).

Check out the layout below:

As you can see, we have 2 controllers and 3 different $scopes in play simultaneously. For instance, if I make changes to "Title" in $scope3, I want those changes reflected in $scope2 as well.

What would be the most effective approach to monitor these changes, transfer values between controllers, and assign them to $scope.value or trigger a new $resource call?

I feel completely overwhelmed by this challenge.

Answer №1

When it comes to handling communication between different scopes, there are various approaches to consider. The most suitable method should be selected based on the relationships between the scopes, whether they are in separate templates or modules, and other factors.

One approach is to create a mediator service that can store the necessary value and be injected wherever required - such as in controllers, other services, or directives.

In Module 1 Template 1:

<div ng-controller="Ctrl1">
  <input type="text" ng-model="val">{{ val }}
</div>

Module 1 Controller 1:

app.controller('Ctrl1', ['$scope', 'ValMediator', function($scope, ValMediator) {
 $scope.val = '';

 $scope.$watch(
   function(){
     return ValMediator.getVal();
   },
   function(newVal){
     $scope.val = newVal;
   }
 );

 $scope.$watch('val',
   function(newVal){
     ValMediator.setVal(newVal);
   }
 );

}]);

In Module 2 Template 2:

<div ng-controller="Ctrl2">
  <input type="text" ng-model="val">{{ val }}
</div>  

Module 2 Controller 2:

app.controller('Ctrl2', ['$scope', 'ValMediator',function($scope, ValMediator) {
  $scope.val = '';

  $scope.$watch(
    function(){
      return ValMediator.getVal();
    },
    function(newVal){
      $scope.val = newVal;
    }
  );

  $scope.$watch('val',
    function(newVal){
      ValMediator.setVal(newVal);
  }
 );

 }]);

Mediator Service:

app.factory('ValMediator', function() {
  var val = '';
  return {
    setVal: function(newVal){
      val = newVal;
    },
    getVal: function(){
      return val;
    }
  };
});

To see an example of this implementation, please refer to the first jsBin example. `ValMediator` demonstrates how a service like this can store and expose variables for use by multiple controllers.

=====================

Another option is to emit events through the `$rootScope`. While this method can facilitate cross-module/scope communication, it may clutter the `$rootScope` with unnecessary events. Nevertheless, it's worth considering as a valid approach.

To see this alternative method, please refer to the second jsBin example.

In Module 1 Controller 1:

app.controller('Ctrl1', ['$scope', '$rootScope', function($scope, $rootScope) {
  $scope.val = '';

  $scope.$on('Val/update', function(e, arg){
    console.log('val update 1', arg);
    $scope.val = arg;
  });

  $scope.$watch('val',
    function(newVal, oldVal){
      if (newVal === oldVal) return;
      $rootScope.$broadcast('Val/update', newVal);
  }
);

}]);

In Module 2 Controller 2:

app.controller('Ctrl2', ['$scope', '$rootScope',function($scope, $rootScope) {
  $scope.val = '';

  $scope.$on('Val/update', function(e, arg) {
    console.log('val update 2', arg);
    $scope.val = arg;
  });

  $scope.$watch('val',
    function(newVal, oldVal) {
      if (newVal === oldVal) return;
      $rootScope.$broadcast('Val/update', newVal);
    }
  );

}]);

This concludes the explanation of the second example, where changes are reacted to and updates are published using event broadcasting via `$rootScope`.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Function useAppDispatch is missing a return type

.eslintrc.js module.exports = { root: true, extends: [ '@react-native-community', 'standard-with-typescript', 'plugin:@typescript-eslint/recommended', 'plugin:jest/recommended', 'plugin:p ...

What is the best way to eliminate products that have already been utilized?

Take a look at my code snippet. $(function() { $("#tags input").on({ focusout: function() { var txt = this.value.replace(/[^a-z0-9\+\-\.\#]/ig, ''); // only allow certain characters if (txt) $("<span/& ...

Tips for enabling the TypeScript compiler to locate bokeh's "*.d.ts" files

I recently made the switch from Bokeh's convenient inline extension framework to their npm based out of line build system. I'm currently working on getting my extension to build, but I've noticed that Bokeh organizes all TypeScript *.ts.d fi ...

Trigger the scrolling of one div when another div is scrolled

Link to Jsfiddle I'm attempting to activate my current scroll while I am outside that scroll, specifically in #DivDet Here is the code snippet of what I have tried so far: $("div#DivDet").scroll(function () { // Still trying to figure out what ...

What causes the image to remain unchanged while the rest of the divs update when navigating with the previous, next, or random buttons?

Whenever I interact with the navigation buttons - specifically previous, next, and random - the contents of all div elements change accordingly. The image element, however, remains unchanged. I am encountering an issue where the image does not correspond t ...

Using Angular 1.x directive with a shared scope and a parent controllerAs approach

I'm currently working on a directive that needs to call a specific method declared within the parent controller. Feel free to check out my Plunker example here Within the parent controller, I am utilizing the 'controller as vm' syntax. How ...

Show User-Specific Information Using DataTable

After conducting extensive research, I have been unable to find a suitable example to reference. My goal is to customize my DataTable so that it only displays data relevant to the currently logged-in user (admin accounts will have access to all data). I am ...

Interactive website featuring 3DSMax .obj and .max models with advanced AJAX clickable functionalities

I have a 3DSMax model in both .obj and .max file formats. This model includes clickable points that work perfectly within 3DSMax, displaying details when clicked. My goal is to showcase this interactive model on the web with all its clickable features int ...

SCRIPT438: The operation failed because the object does not have the ability to use the 'forEach' property or method

Issue with IE8: Property or method 'forEach' not supported $('.tabs').tabs(); $('#search-consumables [data-ajax-call]').change(function() { var $this = $(this), settings = $this.data(), $target = $(setti ...

Is the value of useState not updating even after setting it in a callback function?

I am facing an issue where the error message is not being added to the error state when an error occurs. The backend sends the error message in JSON format { "error": "error message" }, and even though I can log this error message successfully, the error ...

The Cordova Whitelist Extension

After countless attempts and searches online, I am still struggling with a common issue that many have faced before. The goal is to send data to a server via POST request and receive a basic response in return. To enable access to external resources from ...

Validation errors are returned by express-validator duplicated

I am working on validating the request object using Express-Validator. Suppose I have two routes: a GET /users/:id route (fetchUserById) and a POST /users route (createUser). this.router = express.Router(); this.router.route('/').post(this.userR ...

Is there a way to modify the document title using .ready()?

As I work with nested layouts in Ruby on Rails, one particular layout requires me to extract a string from a div and use it as the title of the document. What is the proper method (if there is one) to accomplish this task? <script type="text/javascri ...

invisible recaptcha with synchronous ajax

Hey, I'm trying to figure out a solution on how to obtain the token or a valid response from Recaptcha and then proceed with running the ajax call. Does anyone have any suggestions on how to achieve this in a synchronous manner? Here's the proce ...

Flipping the camera rotation matrix in three.js

I am struggling with a scenario involving objects and a camera being controlled by a trackball. Whenever I add a new object to the main object, I want it to maintain its original orientation regardless of how the camera has moved around. For instance, with ...

Storing state or data in React for persistence when users navigate between pages or use the browser's back

In the process of developing a small SNS app with React (Gatsby.js), I encountered a challenge. My goal is to maintain state in previous pages even when navigating back using the browser, similar to popular platforms like Twitter and Instagram. However, I ...

Manage the angularJS user interface switch through an external event

I have implemented an AngularJS Material UI switch and I am looking to update its status based on an external event. This event occurs when a MQTT message is received on a specific topic that is published. To achieve this, I am utilizing a Node.js MQTT cli ...

What is the basic structure of a JSON-like object?

How can data be effectively stored in a JSON-like structure? I have noticed two different approaches to storing data within a json object, each with its own method for accessing the data (illustrated using examples in Python): Approach 1: obj1 = [ {" ...

When implementing tooltips in Apexchart's JavaScript, the y-axis second does not appear in the chart

Typically, when I append data to the second y-axis as an array, it works perfectly. However, for some reason, when I try to append a collection to the data, it no longer shows with two y-axes simultaneously. I even tried adding x and y as the Apex documen ...

The THREE.LineDashedMaterial is experiencing issues with displaying dashes

I'm struggling to make THREE.LineDashedMaterial work properly in my three.js project. I've tried it with both r73 and r74, but the dashes just won't show up. Here's a snippet of my code: var segmentCount = 200; var radius = 100; var ge ...