AngularJS uses the syntax `Track by $index` to keep track

I'm currently working on incorporating different dates using a datepicker in my application. I have implemented a checkbox, and if the user checks that checkbox, then the dates are added. However, it appears that track by index is not functioning correctly.

Here's the code snippet:

<div class="form-group">       
   <label>6. Enter Onsite Details( Up to 4)</label> <br>
      <div ng-repeat="selecting in selects track by $index" >
          <div class="datePicker">  
              <button 
                 bs-datepicker 
                 class="btn btn-info" id="comm_cal_bttn"
                 name="date"
                 data-animation="am-flip-x"
                 data-autoclose="true"

                 ng-blur="getMinTime(index);"
                 ng-model="selects[index].commDate">
                    <i class="fa fa-fw fa-calendar" aria-hidden="true"></i> Date
               </button>
               &nbsp;
               <p ng-show="selects[index].commDate">
                  {{selects[index].commDate}} <span class="text-muted"> {{selects[index].commDate}}</span> {{combinedTime.format('h:mm A')}}
               </p>
          </div>
          <input type="checkbox" ng-click="addMore(keyAdd);" ng-model="keyAdd"> 
          <label for="dateCheck">Add Additional</label>

</form>

In the code above, when selecting a date, a function getMinTime(index) is called but the index is displaying as undefined in my controller.

My controller:

$scope.selects = [{commDate : null}];
$scope.getMinTime = function(index){

// here index is showing undefined
}

I need to perform some operations based on the index value. For instance, if I added 4 dates, what should I do next? Any suggestions?

Answer №1

Make sure to pass $index as a parameter instead of index

<!-- other code -->

<div class="form-group">
  <label>6. Enter Onsite Details( Up to 4)</label>
  <br>
  <div ng-repeat="selecting in selects track by $index">
    <div class="datePicker">
      <button bs-datepicker class="btn btn-info" id="comm_cal_bttn" name="date" data-animation="am-flip-x" data-autoclose="true" ng-blur="getMinTime($index);" ng-model="selects[$index].commDate">
        <i class="fa fa-fw fa-calendar" aria-hidden="true"></i> Date
      </button>
      &nbsp;
      <p ng-show="selects[$index].commDate">
        {{selects[$index].commDate}} <span class="text-muted"> {{selects[$index].commDate}}</span> {{combinedTime.format('h:mm A')}}
      </p>
    </div>
    <input type="checkbox" ng-click="addMore(keyAdd);" ng-model="keyAdd">
    <label for="dateCheck">Add Additional</label>

    <!-- other code -->

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

Customizing the date on the DatePicker widget

Can a custom date be manually selected in the DatePicker widget? I am looking to set an initial date in the DatePicker and then provide users with the ability to adjust it. Thank you! ...

How can JavaScript generate arrays filled with randomly generated numbers?

As part of a classroom assignment, I am working on creating a table using only Javascript. The table is set up perfectly, except that every other row displays a random number before moving on to the next row. How can I eliminate this random row? var ta ...

Finding the perfect pairing: How to align counters with objects?

public counter = 0; x0: any; x1: any; x2: any; x3: any; x4: any; next(){ this.counter += 1; this.storage.set("Count", this.counter); console.log(this.counter); this.logic(); } logic(){ //automatic counter here var xNum = JSON.parse(JSON.stri ...

I encountered an issue with rendering static images when attempting to package my node-express app with pkg

Struggling to display an image from the public folder in my express app? I could use some guidance on configuring the path to properly render images or css files within the public folder when creating an executable file using pkg. Here's a snippet of ...

Is there a way to automatically hide a div based on a checkbox value when the page loads?

How can I hide a div in my view when a checkbox is not checked upon page load? <input type="checkbox" class="k-checkbox" id="chkInvoiceStatusActive" asp-for="InvoiceStatus" value="true" /> <input t ...

Arranging functions in descending and ascending order

I am working on a Table component that has an array of columns and data. I need to sort the columns and update the table state accordingly. My method takes two arguments key (column key) and sortable (true or false indicating if the column is sortable or ...

Dynamic form in Ant Design React for calculating total

In my Ant Design dynamic form, I have the capability to input both price and quantity in various rows. I can easily add new rows to the form. Upon submission, I was able to retrieve all the values from the rows using the following code snippet: const o ...

Set up a local storage system to store the background color for my page

I have a unique feature on my website where clicking a button generates a random background color each time. However, I am looking to enhance this by implementing a localstorage function that will remember the last clicked color even after closing or reloa ...

In React, what is the correct term for "selection boxes" when choosing multiple items in Finder?

I'm in search of a React component that allows me to select multiple elements on the screen by clicking, holding, and forming a square around them. I've tried searching for terms like 'selection box' and 'React select elements,&apo ...

Can you help me understand how to ensure the CSS translate feature lands in a specific div, no matter its initial position?

I'm facing a roadblock in my journey to create a card game. The issue arises at the final stage of the Translate function implementation. In this game, the player is dealt 30 cards (I've simplified it to four for ease of programming), and upon cl ...

Tips for modifying the value of an MUI DatePicker in Jest (x-date-pickers)

When it comes to { DatePicker } from '@mui/x-date-pickers': I'm facing a challenge in changing the value using Jest. Let me show you my custom wrapper for DatePicker called DatePickerX: import React, { useState } from 'react'; im ...

Having trouble with setting the chosen option in AngularJS?

Greetings! Here is the HTML code I have for a select drop down. <div class="panelProfileDefault col-md-11" ng-repeat="profile in allPanelData"> <form name="updateProfile" class="setProfile" ng-submit="updateProfile(updateProfile)" novalidate> ...

Experimenting with asynchronous functions in React using Jest

As a newcomer to unit testing, I am working with two files: RestaurantReducer import * as Const from '../constants/Const' const RestaurantReducer = (state = { restaurantList: [] }, action) => { switch (action.type) { case Co ...

Having trouble with a tslint error in Typescript when creating a reducer

I encountered an error while working with a simple reducer in ngRx, specifically with the on() method. https://i.sstatic.net/9fsvj.png In addition, I came across some errors in the reducer_creator.d.ts file: https://i.sstatic.net/sWvMu.png https://i.ss ...

Differentiating Typescript will discard attributes that do not adhere to an Interface

Currently, I am working with an API controller that requires a body parameter as shown below: insertUser(@Body() user: IUser) {} The problem I'm facing is that I can submit an object that includes additional properties not specified in the IUser int ...

What is the method for extracting input from a paragraph in Laravel?

<div class="form-group"> <label for="amount_due" class="col-sm-4 control-label">{{trans('sale.amount_due')}}</label> <div class="col-sm-8"> <p class="form-control-static">Unique text to be added ...

The PHP bootstrap 4 Delete button is experiencing an issue with fetching the ID

I am facing an issue with the delete button and confirmation box feature that I have implemented. The problem seems to be related to transferring the id that I want to delete. Here is the table structure for the Delete button: <tbody> & ...

Setting the root position of a div: How can it be done?

Imagine a scenario where a div element is designed to follow the mouse cursor on the screen. This functionality is achieved by manipulating the document's `mousemove` event and adjusting the div's `left` and `top` positions based on the event dat ...

Simple steps to activate and monitor global events using EaselJS

Can EventDispatcher be used to create a global event that any object in the hierarchy can listen and respond to? What is the best approach to implement this in EaselJS, and is it advisable from a general perspective? ...

What steps should I take to verify the validity of an Angular form?

I am struggling with validating an inscription form in HTML. Despite trying to implement validations, the inputs are still being saved in the database even when they are empty. Here are the validations I want to include: All inputs are required Email addr ...