Interacting with directive scope beyond controller boundaries

Developing a custom directive within a controller and implementing it in an ng-repeat loop:

HTML:

<div ng-controller="TestCtrl">
  <div ng-repeat="page in pages">
   <custom 
    load-data="loadData = fn"></custom>
  </div>
</div>  

JS:
The loadData function within the Test directive is invoked in the following manner:

scope: {
   loadData: "&"
}
controller: ['$scope', '$element', '$timeout', '$filter', function ($scope, $element, $timeout, $filter) {
$scope.loadData({ fn: function(data) {  
 //Data calculation.
}});
}  

The invocation of loadData from TestCtrl looks like this:

App.controller('TestCtrl', function($scope, $http, $timeout, $rootScope) {
 $scope.loadData(data);
}  

Although I'm trying to execute the loadData function, it results in an error stating undefined is not a function. Is there a way to access the child directive's scope externally? I've researched using ng-repeat modifying the scope, but haven't found a solution yet. Attempted methods like $broadcast and $on didn't provide any help.

If anyone could offer assistance, it would be greatly appreciated.

Thank you in advance.

Answer №1

I am having trouble understanding your query. Your code seems unclear to me as the data variable is not defined (controller: line 2) and the fn function is also missing (html: line 4).

You are receiving the error undefined is not a function probably because you have not defined the $scope.loadData method.

In an attempt to assist, I have created this code snippet:

angular.module('demo', []);

angular.module('demo')
  .directive('custom', function () {
    return {
      restrict: 'E',
      template: '<div>{{ data || "loading..." }}</div>',
      scope: {
        loadData: '=',
        page: '='
      },
      link: function (scope) {
        scope.loadData(scope.page, function (data) {
          scope.data = data;
        });
      }
    };
  });

angular.module('demo')
  .controller('MainCtrl', function ($scope, $timeout) {
  
    $scope.loadData = function (page, done) {
      $timeout(function () {
        done('data loaded data from ' + page.name);
      }, 1000);
    };
  
    $scope.pages = [
      { name: 'page 1' },
      { name: 'page 2' },
      { name: 'page 3' }
    ];
  
  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.js"></script>

<div ng-app="demo" ng-controller="MainCtrl">
  
  <div ng-repeat="page in pages">
    <custom load-data="loadData" page="page"></custom>
  </div>
  
</div>

Hopefully, this helps clarify things for you.

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

Prevent the use of exponential notation with double numbers in GWT

Is there a way to remove the exponent from a double on the client side in GWT? public double evaluate(final double leftOperand, final double rightOperand) { Double rtnValue = new Double(leftOperand * rightOperand); //Need code to remove expone ...

What is the best way for an object to recognize if its value matches a key in another object, and then retrieve that value

Is there a way to make an object detect if its value is equal to another object's key and then retrieve it? Let's consider the following object named: AnswerString let AnswerString ={ 0: "B", 1: "x" } and the answers are in different objects, ...

"Implementing an ellipsis feature in Highcharts to handle lengthy data gracefully

Looking for some help with adjusting the positioning of a pie chart on the page. The data on the left and right sides is not displaying correctly, so I want to center the chart and add ellipsis for any overflow. Check out the code here: http://jsfiddle.n ...

Unable to process form submission with AngularJS + Stormpath

I am facing an issue with form submission. Even though I believe that the login and password data are being sent correctly, nothing happens when I submit the form. I am attempting to submit the form without using ngSubmit because it is not feasible in my s ...

Function for triggering character or camera movement upon the pressing of a button

Just starting out here with web coding and I'm trying to create a function that moves my game character when a button is pressed. The idea is to change a boolean value to "true" when the button is pressed down so that the character moves. I've at ...

Maximizing Angular JS capabilities within ASP.NET Master Pages

I am currently working on integrating Angular JS with an ASP.NET Application. Initially, I set up the app for my Main Page (Master Page) and everything was running smoothly. However, when it comes to other pages, I encountered several issues. When defini ...

Retrieve class attributes within callback function

I have integrated the plugin from https://github.com/blinkmobile/cordova-plugin-sketch into my Ionic 3 project. One remaining crucial task is to extract the result from the callback functions so that I can continue working with it. Below is a snippet of ...

A guide on how to modify a CSS property to set the display to none upon clicking a radio button

Currently, I am attempting to display two input boxes in a table when I select specific radio buttons and hide them if I choose another option. The code I have does work but has a minor issue, function disappear() { document.getElementsByClassName("ta ...

Updating CSS classes based on conditions in Angular 2

I have a view with three buttons, and initially the content of the first button is displayed. When a button is clicked, it becomes active, but on page load, the initial button does not have the active state. To set the first button as active upon page load ...

The element does not recognize the property 'width' since it is not defined in the type of 'GlobalEventHandlers'

I'm trying to determine the size of an image using JavaScript, but I encountered a TypeScript error: const img = new Image(); img.onload = function() { alert(this.width + 'x' + this.height); } img.src = 'http://www.google.com/intl/en_ ...

My Jquery "animate" is only triggered once instead of on each function call. What could be causing this issue?

I have set my navigation to dock at a specific document height on the top of the viewport, and when it docks, it should display a dropdown animation. $(document).scroll(function(){ var x = $(window).scrollTop(); if(x>=700){ $("header ...

Steps for showcasing smaller grids underneath a main grid

Hey there! I'm fairly new to the world of development and have recently started working with React. I've come across a scenario that has me stumped, so I thought I'd reach out for some assistance. In the image below (you can view it here), ...

What is the best way to insert an object into an array based on a specific condition?

I attempted it like this: <script type="text/javascript"> var clubs = [ {id: 1, name : 'chelsea'}, {id: 2, name : 'city'}, {id: 3, name : 'liverpool'} ]; var newClub = {id: 4, name: ...

What is the best way to use an angular expression in markup to filter down to a specific item within an array?

I need to bind a single element to a specific item in an array and ng-repeat doesn't appear to be the solution. Is there a way to achieve something similar to the following, where I can bind to a specific item in an array? <p class="bottomline"&g ...

When the click event is triggered on the modal, the page suddenly jumps to the top while applying the style of hiding the element

After implementing a modal that appears upon clicking an element on my webpage, I encountered an issue when trying to close it. I added an event listener to the 'close' link element in the top right corner of the modal with the following code sni ...

Having issues with the onSubmit event not being triggered for a react + material-ui form

I am currently using the mui box react component with the form set to the component property and an onSubmit attribute pointing to the submit handler I want to invoke. There is a submit button located at the bottom of the form. However, every time I click ...

Is it possible for a synchronous (blocking) ajax request to delay the rendering of a website's UI?

This query pertains to jQuery, though not exclusively focused on it. To sum it up: Can a synchronous ajax call prevent a regular button from being clicked on? My experimentation shows no issues, but compatibility with other browsers could be a concern. ...

Facebook's Thumbs Down to My Code

I've been struggling to integrate a Facebook Like button on my blog using the following code: $("#fblike").append(" <iframe src='http://www.facebook.com/plugins/like.php?app_id=217624258276389&amp;" + window.location.href + "&amp;send ...

What possible reasons could be causing the ng-show directive to malfunction?

This form displays contact information. <div class="row" ng-controller="contactsCtrl"> <form ng-show="addFormShow"> <h3>Add Contact</h3> <!-- Add form --> <div class="row"> <div class="large ...

Removing and Cloning Elements with jQuery

segment, here is the unique HTML code I have: exam 456<br> test <span class="highlight" id="2" onclick="test2(this);">shez <ul class="spellcheck_recommend"> <li data-wordid="2" onclick="test(this);">shedding</li& ...