Guide to obtaining scope within a nested directive

My problem lies in a nested directive within my code. I am attempting to pass a function from the nested directive to my controller, but for some reason, the function is not being triggered.

Here is an excerpt of my main directive HTML:

<div class="panel panel-primary">
  <div class="panel-heading" ng-click="collapse()">{{user.name}}</div>
  <div class="panel-body" ng-hide="collapsed">
    <address></address>
    <div>
      <h4>Friends:</h4>
      <div ng-repeat="friend in user.friends">
        {{friend}}
        <remove-friend method="removeFri(friend)"></remove-friend>
      </div>
      <div ng-show="!!user.rank">
        {{user.rank}}
      </div>
      <button ng-show="!user.rank"  class="btn btn-primary" ng-click="knightme(user)">Click Me</button>
    </div>
  </div>
</div>

Within the above directive, there is a remove-friend directive that includes a property called method which references a controller function removeFri().

In my directive, I reference it as follows:

.directive("removeFriend", function(){
      return {
        restrict: 'E',
        templateUrl: "removeFriend.html",
        scope : {
          method: "&"
        },
        controller: function($scope){
          $scope.remove = function(){
            $scope.method();
          };
        }
      };
  });

The issue arises when $scope.method() is never called. Even attempting to use alert($scope.method()) results in undefined. Clicking on the x (remove icon) should trigger the removal of a specific item, but this functionality is not working as expected.

For more details and to see the problem in action, please visit the following plunker link: https://plnkr.co/edit/d5fStmjspGm5AMmCK9Rx?p=preview

Answer №1

The userInfoCard directive utilizes an isolated scope. If the function from the controller is not passed to it, it will be undefined. To resolve this issue, you should include it in the scope using = binding. This approach allows for flexibility as you may not know the parameter in advance. Here's how you can set it up:

HTML:

<user-info-card user="user" collapse="true" remove-fri="removeFri"></user-info-card>

Directive:

scope: {
    user: '=',
    removeFri: '=',
    initiallyCollapsed: '@collapse'
},

Plnkr

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

The alignment of the JQuery Mobile tabs is off

As I work on developing a Cordova app, I've noticed that the tabs on my Android phone display in an unusual way. Here's what they look like: The image above was created in JSFiddle using JQuery 2.1.0 and JQM 1.4.2 selected with the following co ...

Unable to view the most recent state

Looking at the code below: export function loginUserRequest() { console.log('ACTION INITIATED'); return { type: LOGIN_USER_REQUEST, }; } we can see the matching reducer: export default function loginReducer(state = initialState, acti ...

"Utilizing Jest Globals to Provide an Empty Input for a Function: A Step-by-

I have developed a function that outputs null when the input is empty: const testFunc = (param) => { if (param) { //blabla } return null; }; To verify the return null behavior with an empty param, I want to utilize describe...it...: describe( ...

Why am I unable to utilize methods in Vue.js?

Hey everyone, I'm currently working on a website that includes a home page and a specific 'Brazil' component. To switch between the two, I am using vue router. Within the Brazil component, there is a calculator feature that should take a gra ...

Issue with Laravel: CKEditor text not loading HTML tags

Could you assist me with this? click here for the image description image description available here ...

Determine in Typescript if a value is a string or not

In my code, I have a component: export type InputData = string | number | null; interface InputData { data?: string | number | null; validate: boolean; } const App: React.FC<InputData> = ({ data = '', validate = true, }) => ...

Creating a sleek navigation bar and sliding feature within the header of your Ionic 2

I am looking to create a sliding header for my website where the gallery hides and the navbar moves to the top as I scroll down, similar to the gif provided. Any help or ideas on how to achieve this would be greatly appreciated. Thank you. https://i.sstat ...

Creating an HTML table using an array of objects

I'm currently working on creating a function that will generate an HTML table from an array of objects. The array provided below is what I need to convert into a table. let units = [ { 'code': 'COMP2110', &apos ...

Adding JavaScript files to a project in Ionic2 with Angular2 integration

I'm looking to incorporate jQuery into my Ionic2 app, which requires loading several JavaScript files: <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <script type="text/j ...

Keep deciphering in a loop until the URI string matches

I have a task to decode a string URI until there are no more changes. The string URI I am working with typically has around 53,000 characters, so the comparison needs to be fast. For demonstration purposes, I have used a shortened version of the string. B ...

Attempting to display two separate d3 line graphs within a single Ionic2 page

I am facing an issue with including multiple similar graphs on a single page within an Ionic2 application. I am utilizing the d3-ng2-service to wrap the d3 types for Angular2. The problem arises when attempting to place two graphs in separate div elements ...

VueJS: components unable to access global variables

I am currently in the process of transitioning my front-end to a gulp-based application. I seem to be encountering some issues with Vue.js as I am facing two errors: [Vue warn]: Property or method "params" is not defined on the instance but referenced dur ...

Upgrading object based on dynamic data shifts in Vue using Vuex

My current task involves updating data in a component based on the selection made from a tabs list at the top of the page. The data is sourced from a Vuex data store, and after conducting tests on the data store, everything appears to be functioning correc ...

Retrieving dates from a database and populating them into a jQuery UI Picker using PHP

I need to retrieve dates from the database using PHP and highlight them in a datepicker. Here is how I am attempting to accomplish this: HTML Date: <input type="text" id="datepicker"> // Static dates // An array of dates var eve ...

Transfer or duplicate an SVG image from the bottom div to the top div

Is there a way to move a div from the chart_div to the movehere div? I've tried duplicating it below the current svg, but I'm having trouble targeting just the header row ('g' element) specifically. Here is the HTML: <script type= ...

Utilizing PHP to dynamically load HTML forms and leveraging JQuery for form submissions

I am currently facing a dilemma that I am unsure how to approach. It seems that JQuery requires unique ID's in order to be called in the document ready function. I use PHP to read my MySQL table and print out HTML forms, each with a button that adds a ...

Unable to locate the MoreVert icon in Material UI interface

I am trying to incorporate the MoreVert icon into my application's header to display signout and settings options. I have added the MoreVert icon as shown below, and although the popup appears when clicking on the supposed location of the icon, I am u ...

Creating an external link in Angular with query parameters

I have created an app where users have their addresses listed, and I want to implement a feature that allows me to open Google Maps when clicking on the address. However, I am currently facing an issue where instead of getting the actual value of {{ this. ...

Discover the power of AngularJS's ng-route feature for creating a dynamic and seamless one-page HTML template experience

I'm attempting to create a dynamic header using ng-repeat. Here's the initial code: <ul class="right"> <li><a href="#carousel">Home</a></li> <li><a href="#about-us">About Us</a></li> &l ...

Using an Ajax request to fetch and display warning information

Exploring the world of MVC and Ajax, I am attempting to generate an Ajax query that will display one of three messages (High risk, Medium Risk, and No Risk) in a div when an integer is inputted. Here's my JSON method: public JsonResult warningsIOPL ...