Is it possible to access an object named foo
that is defined/instantiated inside the link function of directive A from the link function of another separate directive?
Is it possible to access an object named foo
that is defined/instantiated inside the link function of directive A from the link function of another separate directive?
Quote from documentation:
Prior to the pre-linking phase, the controller is instantiated and shared with other directives via the require attribute. This allows for communication between directives and enhancement of each other's functionality.
Essentially, in order to share data between two directives on the same object or its children, you must expose 'foo' within directive A's controller, injected using the require
option in directive B.
The structure of the directives would be like this:
.directive("dirA", function () {
return {
controller: function ($scope, $element, $attrs) {
},
link: function ($scope, $element, $attrs, controller) {
controller.foo = $attrs.dirA;
}
}
})
.directive("dirB", function () {
return {
link: function ($scope, $el, $attr, controller) {
$scope.shared = controller.foo;
},
require: "dirA"
}
})
Check out a working example here.
Due to my limited reputation, I am unable to leave a comment, so I will include it in this response instead.
Here is some extra information for individuals who are facing the same challenge. The outcome may vary based on the placement of the directives in relation to one another.
If the ^ prefix is used, the directive will search for the controller among its parent elements (without the ^ prefix, the directive would only look within its own element).
Looking to sync Javascript events with the BPM of music for a game similar to "Guitar Hero." We start with: In order to create a track file based on beat detection (with each BPM saved like sheet music), how can it be generated beforehand rather than in ...
Currently, I am delving into the world of Node.js, utilizing Express with Jade and Mongoose as my primary tools. Originating from a background in PHP, transitioning to Python, and embracing MVC principles through Django, my aspiration is to create a multip ...
While using MaterialUI's ButtonGroup for a dropdown menu, I encountered an issue trying to set up a series of CTAs that are easily interchangeable within it. The goal is to have all components reusable and the choices in the dropdown dynamic. const C ...
I've run into a problem while working on an assignment. My goal is to show the checkbox values for toppings in the alert box popup upon submission. However, only the text box and radio values are displaying, while the checkbox returns as blank. Any ...
Imagine you have an element like this... <math xmlns="http://www.w3.org/1998/Math/MathML"> <mo class="symbol">α</mo> </math> Is there a method to retrieve the Unicode/hex value of alpha α, which is α, using JavaScrip ...
Greetings, I am currently utilizing AngularJS for a PHP project. One of the features I have implemented is using ngclick for an anchor link, which looks like this: <a href="http://localhost/mediaads/signup" ng-click="showregister($event)">Don't ...
Currently, I am facing an issue with my product page. The metadata for this page is fetched from the backend using an API that retrieves data from a database. To fetch this data and update it on the client side, I am utilizing a custom hook. However, the p ...
I am currently using the Ionic Framework along with its AngularJS UI-Router and $stateProvider to manage different views within my application. However, I am facing challenges in specifying to the $stateProvider that I have multiple "Main Views", each of ...
I want to add a unique effect to my site inspired by the AnimatedHeaderBackgrounds demo available at this link. My twist on this effect involves using upward-moving triangles instead of circles. I've explored various resources, including Stack Overfl ...
When I attempted to retrieve the value using the question mark operator instead of using return twice, I encountered an error stating "Cannot read property 'omitDescription' of undefined." buttonText() { return this.omitDescription ? 'プ ...
I am looking to create a form that can extract field values from existing JSON data. The JSON I have is nested with dictionary structures, but I would like to convert them into arrays. Is there a way to write a recursive function that can retrieve the key ...
For the last 3 months, I have been immersed in developing with Three.js, successfully using it across various browsers and computers. However, a week ago, an error started popping up on my home computer whenever I attempted to load my website: three.js:29 ...
I'm currently working on integrating a single location map using Google Maps in Vue 2 with Vue-google-maps-2. Despite using code that has successfully worked for other parts of the application where multiple markers are plotted from an array, I am enc ...
Looking to verify whether the localStorage gets cleared when I execute my function. Component ngOnInit() { // Logging out when reaching login screen for login purposes this.authService.logout(); } authService logout() { // Removing logged i ...
Hey there, I'm new to working with redux forms and I've been trying to figure out how to use input types other than "text". I've read through the documentation but for some reason, types like "checkbox" or "radio" are not showing up in the b ...
Currently, I am in the process of developing Jest tests for a Node/Express TypeScript backend. Recently, I came across the concept of global test setup which I am integrating to streamline the usage of variables and function calls that are repeated in all ...
I've created a directive that simply converts the text from the controller to uppercase. For some reason, my unit test code is not working. Any suggestions? Here's the html: <div ng-controller="MainCtrl as main"> <div uppercase&g ...
I've successfully created a chained dropdown, but I'm struggling to set initial values for them. Here's what I have so far: <select ng-model="check" ng-options="check.name for check in checks"></select> <select ...
On my website, I have a page named publish.php where users can input details for each image they upload. Each image has its own form, but only one form is displayed at a time in a tabbed layout when the user clicks on the corresponding thumbnail. I want to ...
How does Angular understand which template view to request containing the 'ng-view' element? If I directly navigate within my application to http://localhost/Categories/List/accessories , a request is still sent to '/' or the index ...