Issue: Unable to bind function to expression

I'm struggling to understand why this plunker isn't functioning properly.

The issue lies in my attempt to bind a basic function from a parent controller to a custom directive within a child element. Surprisingly, using = or < works fine, whereas utilizing & does not produce the desired outcome. Even though I acknowledge that this approach may not be best practice, I am puzzled as to why it functions with one method and fails with another. Could there be something obvious that I am overlooking?

Below is the relevant script:

Javascript

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

app.controller('mainCtrl',mainCtrl);
function mainCtrl(){
  var main = this;

  main.test = function(){console.log("test")};
}

app.directive('myDirective',myDirective);
function myDirective(){
  return {
    scope: {},
    controller: myCtrl,
    controllerAs: "dir",
    bindToController: {
      //fn: '&'  //This doesn't work
      fn: '<' // This works
    },
    template: '<md-button ng-click="dir.fn()" class="md-raised">click</md-button>'
  };

  function myCtrl(){

  }
}

HTML

  <div ng-app="myApp" ng-controller="mainCtrl as main">
    <my-directive fn="main.test"></my-directive>
  </div>

Answer №1

& binding gives a wrapper function that can run an expression. Essentially, dir.fn() just retrieves the test property.

According to the official documentation,

& or &attr - allows executing an expression within the parent scope. If no attribute name is specified, it is assumed to be the same as the local name.

To ensure it functions correctly, it should be written like this:

<my-directive fn="main.test()"></my-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

Every attempt to connect with the webservice via an ajax call has ended in failure

I am encountering an issue with a webservice call that I am making using jQuery's AJAX method. Despite receiving JSON data from the webservice when accessed directly through the browser, my site always triggers the error function rather than the succe ...

Puppeteer: Navigate to a specific page number

I'm encountering an issue with a table and page numbers as links, such as: 1, 2, 3, 4 etc… -> each number is a link. Attempted to create a loop to click on different page numbers. On the first iteration, I can reach page 2. However, on the secon ...

issue with fetching response from ajax post call in slim framework 3

Update: The issue has been resolved. The correct localhost address for the ajax request should have been 127.0.0.1 instead of 121.0.0.1 On my client side, I am using the following code to send a POST request to localhost/login. <html> <title> ...

Tips for making an AJAX request using jQuery

I have a new project that involves collecting user data and displaying it on the same page. I was able to successfully implement an Ajax call using JavaScript, but now I want to transition to using jQuery. Below is my JavaScript code: var output1 = docum ...

The d3 drag functionality is only active when the axis ticks are selected

Currently, I am developing a unique package that combines Angular and D3 (v3) and my main focus is on integrating D3's dragging feature into the package. Although I am very close to achieving success, there is a minor bug that I need to address. When ...

Guide to retrieving IDs of images on a canvas during drag and drop functionality

I've developed a finite state machine drawing tool that includes drag-and-drop functionality for different images, such as states and arrows. Each arrow creates a div tag for the transition, with unique ids assigned to every state, arrow, and transiti ...

What is the best way to use toHaveBeenCalledWith with multiple arguments?

Hey there! I'm currently working on a web application using Angular JS. I've been writing unit test cases with the Jasmine framework and trying to mock some services. Unfortunately, I'm facing an issue when trying to call a service with mult ...

Arrange array items according to the values of two attributes

Within my array of N objects, each object is structured with the following properties: { id: 'an id', description: 'a description', isUser: true/false } My goal is to sort this array based on two criteria: Firstly, any object w ...

Tips for achieving a dimmed content effect on Semantic UI React within a NextJS project

I'm currently facing an issue with incorporating dimmed content into the Semantic UI React sidebar on Next.js... Here is the example of dimmed content on Semantic UI: https://i.sstatic.net/5dOGK.png This is the layout code I am using: import React, ...

What is the best way to programmatically insert new rows into a table

I am currently working with the foundation 5 framework (not sure if this is relevant). I need to ensure that each CELL is treated as a distinct item/value when passing information to another page, and I am unsure of how to approach this issue. It should cr ...

Check for mobile browser without having to refresh the page

Currently, I am facing an issue with closing the sidebar when the user clicks on Click Me button in mobile view using flexbox layout. The problem arises because the page needs to be refreshed for it to recognize if it's in mobile mode or not by utiliz ...

Choose the div element by its id with vanilla JavaScript instead of using the jQuery $ selector in order to utilize the RaphaelJS

Currently, I am utilizing the RaphaelJs' mousedown() method. However, I am encountering a problem as I wish to apply mousedown() on a div that is selected using the $(id) selector of JQuery. In this case, I prefer to use vanilla Js for performance rea ...

JavaScript filter method returns a new array with all elements that

I have an Array of Objects with nested Arrays and Objects. Here's an example: data = [{ "id": 10022, "date": "2017-12-31T03:44:19.963808Z", "bought_beats": [{ "id": 10034, "beat": { "id": 6334, "nam ...

What steps should I take to ensure that the navbar is responsive and mobile-friendly?

After creating a navigation bar in an ejs file, I included it in all the files. The issue is that it looks fine on desktop view but does not respond well on mobile devices. There is some space left on the screen and it's not utilizing the entire width ...

What sets a module apart from a script?

As I delve into the depths of TypeScript documentation to grasp the concept of modules, particularly ES6 modules, I stumbled upon some interesting insights. typescript-modules - this documentation talks about typescript modules and highlights an important ...

Tips for concealing an entire row of a table with Jquery

I am currently working on a system that involves a table with anchor tags named original and copy in each row. By clicking on these anchor tags, we are able to update the database whether the item is an original or a copy using ajax. However, I am facing a ...

What is the best way to transfer information from an old JSON file to a new JSON file using JavaScript

I have a JSON file and I need to extract the content of "data" where the provider is "AXIS DATA" into a new JSON file. How can this be achieved? Here's what I've attempted: First, I convert it using JSON.parse and then search for the desired da ...

I am experiencing issues with md-no-focus-style not functioning correctly in my configuration

I have a button that triggers the opening of my navigation bar: https://i.sstatic.net/nMr0i.jpg When I click on it, a hitmarker appears: https://i.sstatic.net/OLQaE.jpg The issue is that even after clicking on a navigation item and transitioning to the ...

Failure to register Express Route

I am currently using express and facing some challenges with creating routes using express.Router. Below is my index.js file (npm main file): require('dotenv').config() const express = require('express') const loaders = require('. ...

Display the outline of a translucent image

Looking to create an image reveal effect on my website. I want to display only the outline of a transparent image using brightness, then gradually reveal the full image by removing the brightness effect. Currently, I achieve this with a black outline usi ...