Sending information from a directive to a controller

I'm working on passing a resource from a directive scope to a controller scope by utilizing a callback provided in the view. However, I'm encountering an issue where the argument variable is showing as undefined. Can you help me figure out what I'm missing?

For reference, here's a jsfiddle example of the issue: https://jsfiddle.net/xqknpe5d/

View

<div on-my-event="doStuff(foo)"></div>

Directive

App.directive('myDirective', ['$whateverModule', function($module) {
  return {
    restrict: 'A',
    scope: {
      onMyEvent: '&'
    },
    link: function(scope, element, attrs) {          
      (scope.onMyEvent) && ( scope.onMyEvent('moo') );         
    }
  };
}]);

Controller

ctrls.controller('myController', ['$scope', function($scope) {
  $scope.doStuff = function(foo) {
    console.log('moo: ', foo);
  };
}]);

Any assistance would be greatly appreciated. Thank you.

Answer №1

To call the function onMyEvent within the link function of your directive, make sure to pass an object with the parameter name ('foo') as the key and the value 'moo'.

link: function(scope, element, attrs) {          
        (scope.onMyEvent) && ( scope.onMyEvent({foo: 'moo'}) );         
    }

For more information on & binding, check out the official documentation.

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

Pressing the reset button will restore the table to its original

As a new React developer with experience mainly in hooks, I have been struggling to find a good example involving hooks. Currently, I am working on implementing an antd table with search functionality. My question is, when a user types something into the ...

How can I organize data from A to Z in alphabetical order in React Native when the user chooses the A to Z option from the dropdown menu?

I am working on a screen that can display up to 1000 data retrieved from the API. Here is the image: https://i.sstatic.net/ErbDD.png Now, I have implemented a drop-down box where users can select alphabetically from A to Z. After selecting an alphabetic ...

Accessing a form from a controller in code is a simple task

Context: I'm developing an AngularJs wizard-style application with 3 steps. The wizard steps are comprised of 4 uiRouter states that share a single controller. The first state acts as an abstract view featuring bootstrap pill navigation and a ui-view ...

Discovering the difference between a singular array and an array of arrays

x = [1, 2,3, 5]; y = [1, [2], [3, [[4]]],[5,6]])); I am currently facing a challenge in finding the difference between these two arrays. function findArrayDifference(arr1, arr2) { var tempArr = [], difference = []; for (var i = 0; i < arr1.l ...

AngularJS is throwing an error claiming that the controller is not defined and is not a function

Struggling to create a basic angular application, every time I attempt it, I encounter this issue and can never find a solution. The content of App.js is as follows: angular.module('Euclid', ['ui.bootstrap', 'ngRo ...

What is the best way to restrict users from inputting numbers larger than four?

I need help with a directive that only allows users to enter numbers in an input text box. The directive "numbers-only" restricts users from entering anything other than numbers. However, I also want to limit the user from entering numbers larger than 4. ...

Having trouble installing @angular/cli 4 on Debian?

I'm having trouble installing @angular/cli on my Debian box. I already have the latest versions of Node.js and npm installed. Interestingly, Angular4 works perfectly fine on my Windows machine where I use it daily. However, when I try to get it runnin ...

Looking to organize data based on duplicate values within an array using Javascript in the Vue framework

Can anyone provide assistance? Data = [{"name":A,"val":20}, {"name":B,"val":7}, {"name":C,"val":20}, {"name":D,"val":8}, {"name":E,"val":5}] SortedValue ...

How to retrieve the present value from an array using AngularJS

Struggling to assign the current user from my list Here is my array after submitting the form [{"name":"Erich","surname":"Josh","email":"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="096c67497a7a276a6664">[email prot ...

Building a local database using the MVC framework concept

Can a locally stored database be developed using MVC framework principles in javascript and html5? Thank you Ravindran ...

Tips for transferring an array from a form to a URL using JavaScript?

My form uses a get method, and includes a select element allowing users to choose multiple options. However, when the user submits the form, the URL does not neatly display these selections. Is there a way for me to pass the array in a more organized manne ...

Inspecting Facebook links

Currently working on a website and interested in incorporating a feature similar to what Facebook has. I'm referring to the link inspector, but I'm not entirely sure if that's its official name. Allow me to provide an example to clarify my r ...

Vue 2.0: Exploring the Power of Directive Parameter Attributes

It has come to my attention that directive param attributes have been phased out in Vue.js 2.0. As a result, I am unable to use syntax like v-model="msg" number within an input tag. Are there alternative methods to achieve the same outcomes without relyi ...

Creating multiple asynchronous calls within a loop in JavaScript

I am currently working on a task in my gulpfile.js that involves uploading an app using Gulp and SharePoint. 'use strict'; const gulp = require('gulp'); const build = require('@microsoft/sp-build-web'); const spsync = require ...

Modify the colors of multiple texts using a concise code block of fewer than 20 lines

I have 8 paragraphs and I want to toggle the class 'one-active' when clicking on each of them, while removing the active class from the others. I currently have the code working for two paragraphs, but it's too lengthy for all eight. How can ...

What is the method to create all possible combinations from the keys of a JSON object?

How can I generate object B that includes all combinations of object A using a key-value pair? { "x": "data-x", "y": "data-y", "z": "data-z" } The desired output should look like this: { ...

Tips for sending query string parameters to an AJAX request using data

Currently, I am making an ajax call to hit the API and I need to include a query parameter. How can I accomplish this with my ajax call? Below is the code snippet that I am using. Any assistance on this matter would be greatly appreciated. Thank you. ...

Parsing JSON data using a regular expression

Within my JavaScript file lies a plethora of object literals: // lots of irrelevant code oneParticularFunction({ key1: "string value", key2: 12345, key3: "strings which may contain ({ arbitrary characters })" }); // more irrelevant code My ta ...

VSCode's Formatting Feature Adds Spaces and Line Breaks to Angular HTML in Certain Files

When I try to use the Auto Formatter on certain HTML files in my Angular/MEAN Stack app, it causes some strange issues with the angular code. The main problems are as follows: Extra spaces added at the end of quoted attributes <div class="hello"> ...

What is the best way to transform object request data into a string in an Express application using Node.js

I am trying to save the request data from app.get('/') to a variable, but I keep getting an error "TypeError: Converting circular structure to JSON". var express = require('express') var app = express() var bodyParser = require('b ...