Accessing the current clicked item in $scope using an Angular Directive within ng-repeat

I have set up a custom directive called calendar which includes a date picker created in JQuery. To associate the ng-model with the date picker, I am using the following code successfully:

var clickedID = "#" + $scope.indexid + "_datePicker";
$(clickedID).trigger('input');

The issue lies in the fact that $scope.indexid always displays the last value in the array of calendar directives.

For instance, if my variables are represented as follows in the directive:

[
  { indexid: 1},
  { indexid: 2},
  { indexid: 3}
]

Whenever any calendar on the page is clicked, $scope.indexid consistently returns 3 (the final item in the array). What steps can I take to resolve this problem?

Describing this dilemma proves difficult; please feel free to request additional information as needed.

EDIT - Additional code:

//Index Page

<div ng-repeat="item in items">

 <calendar indexid="{{$index}}"></calendar>

</div>


//In the Directive
scope: {
  ngModel: "=",
  indexid: "@"
}

//In Directive Controller
console.log($scope.indexid);   

//In the directive template
<input type="text" ng-model="testModel" id="{{::indexid}}"/>

Answer №1

When iterating through an array and needing to modify each object within it, you must pass in the object by reference in order to make changes. Here's how:

<div ng-repeat="element in elements">
  <custom-element item="element"></custom-element>
</div>

The variable item will represent the current element in the array, so your modifications should be done based on that.

In your component directive, structure it like this:

scope: {
  item: "=" // reference to 'item' from the parent scope...the loop
},
template: '<input type="text" ng-model="testModel" id="{{::item.id}}"/>',
link: function ($scope) {
  console.log($scope.item.id);

  console.log($scope.testModel); // value of the input field
}

This approach allows for easier access to each individual item in the array. Passing it by reference enables writing back to it; any changes made to item within the directive will be reflected in the elements array in the main scope.

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 on using JQuery to extract form field information from a drop-down menu, display it in a div, and then compare it with the subsequently

In my HTML file, I am using two dropdown lists and JQuery for validation. First, I need to save the selected items from both dropdown lists in a variable and then compare them with the next selection. If the data from both dropdown lists match, an alert m ...

Angular filter is causing an error message saying "Unable to interpolate," but the filter is functioning as expected

I have implemented the filter below (which I found on StackOverflow) that works perfectly fine on one page. However, when using the same object, it throws an error as shown below: app.filter('dateFormat', function dateFormat($filter){ return f ...

Error: Unable to access the 'version' property of null

Having trouble installing any software on my computer, I've attempted various solutions suggested here but none have been successful. $ npm install axios npm ERR! Cannot read property '**version**' of null npm ERR! A complete log of this ru ...

Issue with npm installation leading to missing node_modules directory

When attempting to run npm install . in a local directory, I keep encountering the following errors: npm ERR! install Couldn't read dependencies npm ERR! Darwin 15.2.0 npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "install" "." npm ERR! no ...

What is the best way to validate the body object when working with Actions2 in sails.js?

Just starting out with sails.js I understand that the inputs object allows actions2 to validate the request parameters. However, how can I access and validate the request body? For example, req.body. I know I can access this from this.req.body, but I was ...

Utilizing ngClassEven and ngClassOdd in Angular 2 for Improved Styling

I attempted to replicate the behavior of ng-class-even and ng-class-odd (originally from Angular 1) in my Angular 2 application. Below is the code I wrote and it's functioning correctly, but I'm curious if there are alternative methods to achiev ...

Improving mongo information using angularjs

Have an Angular and MongoDB application. This is a part of my API where I have POST and PUT requests. The POST request works fine, but when I send a PUT request, I get an error "Cannot set property 'typelocal' of undefined". However, the PUT requ ...

Does jqgrid navgrid have an event called "on Refresh"?

Is there a way to trigger an event before the grid automatically refreshes? I am looking for something similar to "onSearch" but for the reset button. Below is the code snippet for the navgrid: $("#jqGrid").jqGrid('navGrid','#jqGridPag ...

Updating AngularJS scope after sending a post request to the API using Restangular

I'm using a controller that utilizes Restangular to load data in the following way: var oneTopic = Restangular.one('topics', topic.id); oneTopic.get({}, {"Authorization" : localStorageService.get('***')}).then(function(topic) { ...

Tips for choosing and deselecting data using jQuery

Is there a way to toggle the selection of data in my code? Currently, when I click on the data it gets selected and a tick image appears. However, I want it so that when I click again on the same data, the tick will disappear. How can I achieve this func ...

Struggling to generate a cookie through an express middleware

I'm currently working on setting up a cookie for new user registrations in my app to track their first login attempt. I came across this thread which provided some guidance but I'm still facing issues. Below is the snippet of my code: // Middle ...

Conceal elements with a single click of a button

How can I use jQuery to hide all li elements with an aria-label containing the word COMPANY when the Search from documents button is clicked? Here is the HTML code: <ul class="ui-autocomplete ui-front ui-menu ui-widget ui-widget-content" id="ui-id-1" t ...

Error callback not being invoked on save() method in AngularJS service

So I am currently using the AngularJS Restful Service $resource in my project. However, when I try to call the $save function and provide an error callback, it does not get invoked. Surprisingly, even though the server sends a 418 error, which is not a suc ...

An abundance of AJAX requests inundating the server

While working on a straightforward AJAX request, I encountered an issue where the server is sending back 3 responses instead of just one (you can see the example in the attached image). Could someone provide insight into why this might be happening? var ...

Tips for spinning HTML5 Canvas's background image and adjusting the zoom with a slider

As someone who is new to the world of coding, I have a few goals in mind: First and foremost, I want to create a canvas. I also aim to add a background image to the canvas. Importantly, this background image should be separate from any foreground images ...

Assign the private members of the class to the arguments of the constructor

class Bar { #one #two #three #four #five #six #seven #eight #nine #ten #eleven #twelve #thirteen #fourteen #fifteen #sixteen constructor( one, two, three, four, five, six, seven, eight, ...

Execute an AJAX call in CodeIgniter using jQuery, and when the call is redirected, display the entire page within a

A peculiar issue arises with the form submission process. When the submit button is clicked, jQuery Ajax triggers a call to the controller for form validation. If the validation fails, the form is redrawn; if it passes, the page redirects to the home page ...

Instructions on converting XML data obtained from an AJAX call into a downloadable file

I've been struggling with converting an AJAX response containing XML text into a downloadable file. I've tried various methods but haven't had success. In the past, I was able to work with a similar scenario involving a pdf, where the conte ...

Executing javascript href using Python in Selenium

Currently, I am attempting to use Selenium in Python to click on a href JavaScript link. The HTML code appears as follows: HTML Example and my goal is to click on javascript:goType(1). This is the approach I have taken: advance_search = browser.find_el ...

Using multiple ng-app directives in AngularJS on a single page

Only the first ng-app works: check out my plunker Is there a way to get both apps to function together? I know I need to add a line to bootstrap the modules, but I'm uncertain of the exact placement for that line. HTML <head> <script src ...