Is there a way to transfer the value from one directive to another directive template and access it in a different directive's scope?

I attempted to pass the directive attribute value to a template ID, which can then be used in another directive.

Below is my index.html code:

<my-value name="jhon"></my-value>

Here is the JavaScript code:

.directive('myValue',function(){
  return {
    restrict:"E",
    templateUrl:"myname.html",
    scope: {
      name:"="
    },
    link:function(scope,element,attr) {
    }
  }
});

This is my template code (myname.html):

<div>
<p  slide heading="name"></p>
</div>

In the above code snippet, "slide" refers to another directive.

Here is the code for the slide directive:

.directive('slide',function(){
  return{
    restrict:"A",
    link:function(scope,elem,attr){
      console.log(attr.heading);
      // I need the name that was assigned in index.html, like "attr.heading = jhon"
    }
  }
})

The issue I am facing is I assigned name="jhon" to the my-value directive. I want to dynamically send that name to the template of the my-value directive, and from there, I need to assign that name to the heading attribute of the slide directive as heading=name. This name should then be utilized in the link function of the slide directive because I aim to pass the name dynamically from one directive to its template and subsequently assign it to another directive's attribute, using it within the slide directive's link function.

Thank you in advance!

Answer №1

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

        app.directive('myValue',function(){
            return {
                restrict:"E",
                template:'<div><p  slide heading="{{name}}"</p></div>',
                scope: {
                    name:"="
                },
                link:function(scope,element,attr) {
                }
            }
        });

        app.directive('slide',function(){
            return{
                restrict:"A",
                link:function(scope,elem,attr){
                    console.log(attr.heading);
                    // Change the below variables as needed
                }
            }
        })

        app.controller('demoCtrl',['$scope',function($scope) {

          $scope.myname = "placeholder"
        }]);
<!DOCTYPE html>
<html lang="en" ng-app="my-app">
<head>
    <meta charset="UTF-8>
    <title>Title</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
    
</head>
<body>
   <div ng-controller="demoCtrl">

       <my-value name='myname'></my-value>
   </div>

</body>
</html>

just change the below lines.

customname.html

<div>
<p  slide heading="{{name}}"></p>
</div>

homepage.html

<my-value name="customname"></my-value>  

here assign a controller variable instead of static name to enable two-way data binding. e.g

app.controller("...",function($scope){
$scope.myname = "newvalue"
})

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

Tips for concealing dynamically generated div elements within a VueJS v-for loop

Is there a way to hide dynamically generated div elements in VueJS? In my chatbot application, messages are passed through a prop called messages. These message arrays create multiple Divs on the screen displaying information. One particular Div is used to ...

The webpage fails to display Vue Bootstrap radio buttons in a filled state

Hey there! I've been working on a project using vuejs and bootstrap-vue-3. My goal is to allow users to print data about the company, so I created a print scss file for that purpose and added some styles. However, I'm facing an issue where the ra ...

Is there a way to assign multiple paths to a single page within the NextJS pages directory?

I'm currently working on a project that has two different themes, and I have the ability to switch between them based on the environment. Everything works perfectly, but I'm encountering an issue with paths. Some pages should open with different ...

change the return value to NaN instead of a number

Hey there, I have something similar to this: var abc1 = 1846; var abc2 = 1649; var abc3 = 174; var abc4 = 27; if(message.toLowerCase() == ('!xyz')) { client.say(channel, `abc1` +`(${+ abc1.toLocaleString()})` +` | abc2 `+`(${+ abc2.toLocaleStri ...

Could anyone help me locate the section in the MUI documentation that explains the correct syntax for the commented code lines I am working on?

Before proceeding, please note that the ThemeProvider with theme={theme} has already been provided. Now, I will share two distinct sets of code files. These files contain sections commented out because they are not functioning as intended when implementing ...

What methods can be used to center a Google map on a specific location?

I am facing an issue with three map canvases on different divs. Despite setting the center to the same position in all three of them using JavaScript, the second and third maps do not seem to reflect that center. Does anyone have any insights on what cou ...

Why is it necessary to omit node_modules from webpack configuration?

Check out this webpack configuration file: module.exports = { mode: "development", entry: "./src/index.ts", output: { filename: "bundle.js" }, resolve: { extensions: [".ts"] }, module: { rules: [ { test: /\.ts/ ...

Step-by-step guide on achieving a radiant glow effect using React Native

I am looking to add a glowing animation effect to both my button and image elements in React Native. Is there a specific animation technique or library that can help achieve this effect? While I have this CSS style for the glow effect, I am uncertain if ...

AngularJS: Enhancing Dropdown Menus

I currently have a pair of drop down lists. The first dropdown looks like this: <select ng-model="vm.eesAdminSetupData.Type" class="form-control" id="Type" name="Type" required> <option value="Form">Form</option> <option value="List ...

AngularJS 2 treats lists as custom directives

I am working on developing an e-commerce website. The project involves displaying a list of goods and a basket (which contains selected goods). In order to achieve this, I have created a controller with two arrays: one for goods (already filled) and anothe ...

Leveraging TypeScript to call controller functions from a directive in AngularJS using the "controller as"

I've encountered an issue while trying to call a controller function from a directive, specifically dealing with the "this" context problem. The logService becomes inaccessible when the function is triggered by the directive. Below is the code for th ...

What could be causing the issue of my Angular service value not reflecting changes in my scope?

Take a look at this simple JSFiddle I created to showcase what I believed would be a dynamic service value that could be monitored by any controller it was injected into. Here's the Angular code: var app = angular.module("application", []); app.serv ...

Update the state both before and after executing the API call

I'm facing an issue with the setState function where it seems to be getting called again before completing the previous batch of state updates. My data object has the following structure: [{ id: 0, loading: false }] On my webpage, I have toggle butt ...

Retrieve data from dropdown menu to showcase table using Node.js

I'm currently diving into learning nodejs and mongodb. My backend setup includes expressjs and mongojs, while ejs is handling the frontend of my application. The main goal is to allow users to select a class from a dropdown menu and view a correspondi ...

What is the best method for incorporating a YouTube embedded video into an image carousel using HTML and CSS?

I'm encountering an issue with my product carousel that includes an image and a video. The image displays correctly, but when I attempt to click on the video to have it appear in the main carousel view, it doesn't function as expected. Here is a ...

Customizing Webpack 4's Entry Point

Below is the layout of my project: -node_modules -src -client -js -styles -views -index.js -webpack.config.js -server -.babelrc -package -package-lock -README.md -webpack ...

the navigation bar vanishes without any dropdown menu to replace it

I've been working on setting up a responsive navbar that collapses into a drop-down menu when the screen size is reduced below a certain number of pixels. So far, I've been able to hide the regular menu when the size is reduced, but when I click ...

Issue with Angular form not triggering ng-submit functionI am facing a problem where

Issue with angular submission: I've been trying to implement a simple submission feature using angular, but the $scope.account_create.submit function isn't getting called upon submission. I have added the name attribute to the form and ensured th ...

What methods can be utilized to avoid displaying a Bootstrap popover intermittently within an AngularJS framework?

When using the bootstrap popover in a custom AngularJS directive, I need to display an error message when the button is disabled by setting $scope.buytypeButton= {type:false}. The error message should be displayed in a popover. On the other hand, when $sco ...

Sharing data across multiple paths

route.post('/register',function(req,res){ //completed registration process // token value assigned as 'abc' }) route.post('/verify',function(req,res){ // How can I retrieve the token ('abc') here? }) I' ...