Methods for obtaining a directive's scope value and utilizing it in another directive within angularjs?

I have a specific requirement to transfer the scope of one directive to another directive and enable two-way binding.

The goal is to update JSON values whenever the ng-model changes. In the example below, the JSON contains rowsets with attributes that need to be bound to controls (textbox) using ng-model=CuId. Therefore, when the value of the corresponding attribute changes, the JSON should automatically update.

Source Code:

var app = angular.module("myApp", []);

app.directive("main", function() {

});

app.directive("sub1", function() {
    return {
        restrict: 'E',
        replace: true,
        template : "<h1>sub1</h1>",
        link: function($scope, element, attrs) {
           $scope.soJSON={
                "entityinfo": {
                  "entity": "Customer29Jan16",
                  "tenantId": "292FEC76-5F1C-486F-85A5-09D88096F098",
                  "timeStamp": "2016-04-07T10:33:38.507Z"
                },
                "collections": {
                  "Customer29Jan16": {
                    "meta": {
                      "parentreference": "***",
                      "pkname": "***",
                      "fkname": "***"
                    },
                    "rowset": [
                      {
                        "CuId": "test",
                        "Name": "test",
                        "Quantity": "test"                       
                      }
                    ],
                    "rowfilter": []
                  }
                }
              }
        }
    };
});

app.directive("sub2", function() {
    return {
        template : "<input ng-model=CuId> <input ng-model=Name> <input ng-model=Quantity>"
    };
});

HTML Code:

<div ng-app="myApp">
<main>
   <sub1>Test<</sub1>
   <sub2>Test<</sub2>
</main>
</div>

JS Fiddle Link : https://jsfiddle.net/bagya1985/23vz1xux/1/

Answer №1

AngularJS offers support for directive controllers, allowing for controllers that can be shared among multiple directives needing the same controller. To achieve this, you would utilize require:'^main' in your child directives. For further details, refer to the following link: https://docs.angularjs.org/guide/directive

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

Invoking an asynchronous operation within another in redux-thunk

I am currently working on a React app that utilizes redux-thunk for handling asynchronous operations. I have two functions named getActivities() and createActivity(). My goal is to call getActivities() after successfully calling createActivity(). However, ...

What is the process of using JavaScript code to read a text file?

Trying to use Google Charts while reading data from a text file. The code in JS is written for this purpose: function readTextFile(file){ var rawFile = new XMLHttpRequest(); rawFile.open("GET", file, false); // using synchronous call var allTe ...

Is it acceptable for a video to autoplay even if it is not connected to the DOM?

Consider the following three scenarios: document.adoptNode, document.importNode, and document.createElement with assigned properties. In all cases, the video autoplay feature is activated even when it's not connected to the DOM. This behavior diffe ...

Testing the functionality of an event that is not linked to an event emitter

Below is the code snippet I'm working with: private client: any; this.client = mqtt.connect(url, mqttOptions); this.client.on('message', (topic: string, message: string) => { console.log('came inside onMessage'); ...

Prevent zooming or controlling the lens in UIWebview while still allowing user selection

Is there a way to disable the zoom/lens on uiwebview without affecting user selection? I'm trying to avoid using "-webkit-user-select:none" in my css. -webkit-user-select:none ...

Determine whether the browser is being used on an Android or iOS device

Is there a dependable method to alter buttons and text on a mobile site based on whether the user is using an Android or iOS browser? ...

Struggling to save a signature created with an HTML5 Canvas to the database

I've been on the hunt for a reliable signature capture script that can save signatures to MySQL, and I finally found one that fits the bill. However, there are two issues that need addressing: The canvas doesn't clear the signature when the c ...

Toggle the visibility of image elements depending on the input entered in the search bar using jQuery

I have a photo gallery grid and I am looking to implement a search functionality for an img tag's 'data-title' value, where only images with matching data will be displayed. For example, if I type "hay" into the search box, only the image be ...

Having difficulty retrieving textbox value through ajax within a php function

There is a text box for weight input where the user can enter the weight and then click a button. Here's the HTML code snippet: <tr> <td> <?php echo "Weight:"; ?> </td> <td> <span class=&ap ...

The validation of Google Recaptcha is malfunctioning when used on a local server

Has anyone successfully validated Google reCAPTCHA on localhost? I'm having issues getting it to work properly. Any recommendations for the best solution? ...

How can I ensure that the ons-scroller stays at the bottom when writing a JavaScript code in Onsen UI?

How can I ensure the ons-scroller stays at the bottom when writing JavaScript? This is the code I am using: <ons-page> <ons-toolbar> <div class="left"><ons-back-button>Return</ons-back-butto ...

Is there a method to enclose a Grafana row with a border?

I recently created a Grafana dashboard using JavaScript and I realized that adding a border around each row would enhance readability. Here is how the current view looks like: https://i.stack.imgur.com/itKYR.png However, it can be difficult to differenti ...

The Ajax request relies on the initial conditions (then)

My goal is to use the initial function to verify if an item exists and if it can be added to the PHP $_session. The aim is to limit the $_Session to a maximum of 3 items, returning 1 if the item already exists in the session. This enables the favorite fun ...

Issue with JavaScript JSON.parse converting string values to Infinity

Does anyone have an explanation for this peculiar behavior I encountered while using the JSON.parse() function in Javascript? Normally, when you pass a string to it, it should generate an error. For example, JSON.parse("5ffc58ed1662010012d45b30" ...

What is the method for retrieving array values from an attribute?

I am currently developing an Angular 6 application and I need to pass and retrieve array values dynamically through attributes. Here is the code snippet I have used for this purpose: HTML: <ul class="list-unstyled" id="list" [attr.parent_id]="123"> ...

Working on asynchronous processing of Highland stream fragments

My current setup involves utilizing highland.js to process a file using a stream and extract content between specific delimiters. Additionally, I am incorporating async.js to perform a sequence of http requests. I am seeking a way to pass the output x fro ...

AngularJS causing issues with Bootstrap Selectpicker functionality

Can someone please assist me with an issue I'm facing? I have encountered a problem while using the bootstrap select picker for a select box. The first selection works perfectly fine, but the issue arises when making a second selection. It seems to be ...

Trigger a re-render in React using setState(null) and forceUpdate()

When using react 16, setState(null) no longer triggers an update according to the official documentation: The new behavior for calling setState with null is that it does not trigger an update. This gives you the control in the updater function to decide ...

Sorting a Javascript table performs effectively, however, the results may vary when iterating through all the indexes

I'm currently using a function to sort a table I have: function ReorderSupplyGP(table){ table.find('tr:not(.kn-table_summary)').sort(function (a, b) { var tda = $(a).find('td:eq(1)').text().trim(); var tdb = $(b).find(&a ...

sending an array from Javascript to PHP

Utilizing JavaScript, I have successfully converted data from a file into an array. Now, my goal is to utilize input from a form to search through this array using PHP and present the results in a table. Despite browsing various solutions for similar probl ...