directive in Angular ordering

After exploring this link, my understanding deepened:

http://plnkr.co/edit/k5fHMU?p=preview

Upon analyzing the code snippet provided in the link above, I noticed that the Angular app consists of both a controller and a directive. However, there seems to be an issue where the variables defined in the directive controller are not accessible in the app controller. How can one effectively utilize the variables from the directive controller within the app controller?

I appreciate any guidance on resolving this matter.

Answer №1

Here is the solution you've been searching for.

After reviewing your Plunker code, I was able to achieve what you desired.

To accomplish the task at hand, Angular Events like $broadcast and $emit were utilized for Controller to Controller communication within Angular.

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
//This section executes only after an event has been emitted by the child controller
  $scope.$on('SecondRun', function(event, data){
    console.log($scope.name);
     $scope.name =  $scope.name + 'First ';
  });
});

app.directive('exampleDirective', function() {
  return {
    restrict: 'E',
    template: '<p>Hello {{name}}!</p>',
    controller: function($scope, $element){
      $scope.name = "Second ";
      //emiting event to parent scopes
      $scope.$emit('SecondRun',true);
    },
    link: function(scope, el, attr) {
      scope.name = scope.name + "Third ";
    }
  }
})

I trust that this explanation will be beneficial for you in resolving your issue.

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 customizing the CSS file of a React component imported from a UI framework

As I embark on building a large application utilizing React, I find myself navigating the new territory of React philosophy coming from a background of Css+Js+jQuery methods. One key requirement for my project is a reliable UI framework that aligns with Go ...

Dealing with Errors using Restangular

I'm struggling with retrieving a JSON object from my backend using Restangular. 1) Everything seems to be working fine, but I am unsure how to handle errors since there is no callback or promise function: meals = restAngular.all('meal').ge ...

What is the best way to dissect emails using Haraka?

After delving into the haraka project (at ), I managed to successfully install it on my linux machine. Now, I'm interested in finding a comprehensive tutorial on parsing email meta headers and content body using haraka. Despite searching through their ...

How to achieve horizontal auto-scrolling in an image gallery with jQuery?

Hey there, I'm currently working on an Image Gallery project. I have arranged thumbnails horizontally in a div below the main images. Take a look at this snapshot img. My goal is to have the thumbnails scroll along with the main pictures as the user ...

Formatting decimals with dots in Angular using the decimal pipe

When using the Angular(4) decimal pipe, I noticed that dots are shown with numbers that have more than 4 digits. However, when the number has exactly 4 digits, the dot is not displayed. For example: <td>USD {{amount| number: '1.2-2'}} < ...

Angular view password reset in Laravel 5.1: Understanding CSRF token validation

I have successfully customized the default Laravel authentication to function as an API for my AngularJS project. So far, everything is running smoothly. Users can go to /reset, enter their email address, receive an email with a password reset link that di ...

Looking to alter the CSS of an ID element when hovering over a link on your website?

Irrespective of the positioning of the links in the html, a simple hover effect can trigger changes like switching images or altering backgrounds anywhere on the website. The ideal solution would involve a straightforward method without the need for Javas ...

Tips for querying orchestrate.io

Recently, I found myself in need of a user-friendly database for a small highscore system in my game development projects using JavaScript. Through the Github student developer pack, I came across Orchestrate.io. After discovering a suitable driver module ...

Check to see if a variable has been assigned a value in the form of a jquery

Scenario: <div> <div class="application-section active"></div> <div class="application-section"></div> </div> I need to define the following variables var $activeSection = $("div.application-section.active") ...

Step-by-step guide on creating a login functionality in Node using the Mean.io stack

I'm currently working on a web app using the MEAN.io stack. I have completed the frontend part with HTML, CSS, and AngularJS along with some logic. Now, I am looking to implement server-side login functionality, but I'm unsure where to begin sinc ...

The first time I tried using React-app, I encountered an error that read "[email protected] postinstall"

I've been struggling to set up create-react-app, encountering the same issue repeatedly. I tried uninstalling and reinstalling node.js, but only the package.json and my-app folder were created. No src or other required folders were generated. Termina ...

Learn how to make a mesh in Three Js React refract its environment while keeping the background hidden from the camera

I've been grappling with this challenge for quite some time now, so any assistance would be highly valued! My aim is to create the illusion of an image being confined within a mesh structure. My initial idea was to utilize a mesh with defined thickne ...

Update the image within the svg tag

I am attempting to modify a preexisting SVG element within an HTML document. Here is the current code: <svg class="logo" viewBox="0 0 435 67"> <!-- IMAGE DIMENSIONS --> <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#logo- ...

Incorporating string input values into a function

I am currently working on a function that retrieves the value of an input upon clicking a button. My goal is to then have another event that will incorporate that value into a function when the button is clicked. var getInput = function() { $('#inpu ...

Can someone guide me on how to extract checkbox values in a post method using Angular

I'm facing an issue with a table that contains a list of rules. Whenever the checkboxes are clicked, I want them to send a "true" value to an API endpoint. However, I keep receiving an error stating that the "associated_rule" is undefined. After tryi ...

Having trouble adding items to an array within a Javascript promise

I am facing an issue with the exported function in a Nextjs app, which acts as an API page. The problem arises when the 'domainnames' array returns nothing in the 200 response. Interestingly, if I exclude the 'GetDomainStatus()' funct ...

Tips for stopping Vue.js automatic merging of CSS classes

Recently, I embarked on my journey with Vue.js and have been thoroughly enjoying the experience. However, I've stumbled upon a challenge that has me stumped. Despite searching high and low and studying the documentation, I haven't found a solutio ...

What is the best way to reset a dropdown list value in angular?

Is there a way to erase the selected value from an Angular dropdown list using either an x button or a clear button? Thank you. Code <div fxFlex fxLayout="row" formGroupName="people"> <mat-form-field appearance=&quo ...

Retrieve the value of a property in a JavaScript object by specifying a dynamic key for the property

As I work on my current project, I find myself immersed in a world of SVG animations. The challenge lies in triggering these animations as the user scrolls down to view the SVGs. To address this, I took the approach of creating functions for each Snap.SVG ...

Using React to trigger a child component's function upon clicking a tab

I need assistance with creating a React app that includes two tabs (Tab1, Tab2). When Tab2 is clicked, I want a message to appear in the console saying 'Function A is called'. Unfortunately, the code I have currently does not display the message ...