Click on the (i)th anchor in Angular2 to pass the data of (i+1)th element

Is it possible to pass the i+1 data when clicking on the i-th anchor in Angular 2 using ngFor?

Could someone please assist me with this?


   <ul>
        <li *ngFor="let menu of _menus; let k=index" >
            <a (click)="GoToSectionPage()">{{menu.Title}}</a>
        </li>

    </ul>

I have used the above code for binding an array of data. Can I pass the k+1th data in the GoToSectionPage() method?

Thank you!

Answer №1

Simply send the index in this way:

(click)="VisitSection(k)"

In your component, implement the following code:

public VisitSection(k: number) {
  const menu = this._menus[k+1];
  //add your custom logic here
}

This should provide assistance

Answer №2

Absolutely, it is possible. Simply include it as an argument in the function call

(click)="NavigateToNextSection(k+1)"

Within the component class, retrieve the passed parameter.

NavigateToNextSection(index: any) {
   console.log(this._sections[index]);
}

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

How can I identify the nearest ancestor based on a specific class in Angular 8?

In order to achieve a specific goal, let's imagine this scenario: Object A Object B Object C Object D Object D represents my angular component. It is important for me to adjust its height based on the heights of Object A and Object B. Regardle ...

Issue with the functionality of Angular reactive custom validator inoperable

Recently, I created a simple validator that compares the values of two date form controls within a form group. The basic rule is that the maturityDate has to be greater than the valueDate, otherwise the form group should be marked as invalid. Here is how ...

React: Updating a property in an array of objects causes properties to become undefined

My intention was simply to update a property within an object inside an array and then update the state of the array. However, I encountered an issue where all properties except the one that was updated became undefined. The code in question is as follows ...

The styling of Primeng Carousel appears to be incorrect

After obtaining a copy of the Primeng v8 carousel component, I noticed that the output is quite different from what is displayed on its official website: <p-carousel dir="ltr" [value]="basicDataService.latestProducts" [numVisible]="4"> <ng-t ...

Properly relocating the node_modules folder: A step-by-step guide

I decided to relocate my node_modules folder to a different location. To do this, I deleted the original node_modules folder and moved the package.json file to the new desired location. After that, I executed the command npm install to install the node_mod ...

What is the best way to specify Next.js Context types in TypeScript?

Can someone help me with defining the types for next js Context and req? Below is the code for the getServerSideProps function- //Server side functions export const getServerSideProps: GetServerSideProps = async (context) => { await getMovies(conte ...

Having trouble updating npm using npm install npm@latest -g and encountering an error?

Encountering errors and warnings while updating npm to the latest version c:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\Extensions\Microsoft\Web Tools\External>npm cache clean --force npm WARN u ...

AngularJS ui-router behaving erratically when manually refreshing the page

While working on an application with my team, we've noticed some inconsistent behavior. When a user refreshes the browser, the page UI state is fully refreshed, but only up to a certain route. Our application starts on the /Home route, establishing t ...

md-table Using FirebaseListObservable as a DataSource

I am currently working with a FirebaseListObservable and a BehaviorSubject that is listening for an input filter. The goal now is to merge these two entities in order to return an array that has been filtered based on the input value, which will then be us ...

Is it possible to initialize multiple Observables/Promises synchronously in ngOnInit()?

I am relatively new to Angular/Typescript and facing a challenge. In my ngOnInit(), I am trying to fetch settings from my backend using a GET request. After that, I need to subscribe to an observable. The observable updates the widgets' content over t ...

What does `(keyof FormValues & string) | string` serve as a purpose for?

Hey there! I'm new to TypeScript and I'm a bit confused about the purpose of (keyof FormValues & string) | string. Can someone please explain it to me? export type FieldValues = Record<string, any>; export type FieldName<FormValues ...

typescript locate within the union type in the mapping expression

Consider the following: type X = { label: 'Xlabel', X_id: 12 }; type Y = { label: 'Ylabel', Y_id: 24 }; type Z = { label: 'Zlabel', Z_id: 36 }; type CharSet = X | Y | Z; I am looking for type CharSetByLabel = Map<CharSet& ...

Create an object type with string keys and corresponding value types by combining a tuple type of string constants and another tuple type of different types

Contemplating how to define types for a concept similar to daggy, where a list of property names in an array leads to the creation of constructors. Thinking of something along these lines: [A, B] with [X, Y] => { [A]: X, [B]: Y } In this scenario, A ...

What is the best way to compare an array with comma-separated values in JavaScript?

I have a scenario where I have two arrays, one for categories and the other for products. Each product contains multiple categories as a comma-separated string. My goal is to match a specific category with each product's product_category value and the ...

The Mongoose getter function is triggering error TS2590 by generating a union type that is too intricate to be displayed

I've come across the TS2590: Expression produces a union type that is too complex to represent error while trying to compile TypeScript. The issue seems to be connected to the id's getter function idFromString, as removing the id getter prevents ...

The installation failed due to an unresolved dependency when running npm install

Upon performing a git clone, I encountered this error with my project after running npm install: npm ERR! code ERESOLVE npm ERR! npm ERR! While resolving: <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6e0f1e1e43081c01001a0b000 ...

Tips for leveraging TypeScript with Redux connected components

How can I properly type props that are connected to a reducer? The following code may produce TypeScript errors but still works during runtime. class Sidebar extends React.Component { constructor(props) { super(props); } render() { return ...

Is there a way to retrieve real-time information from an API by utilizing the HTTP.Get method?

When working with ANGULAR I am faced with the challenge of pulling data from an API using http.get at regular intervals. The API continuously updates minute by minute traffic data, and I need Angular to display these live updates. Currently, my approach ...

Strategies for redirecting search queries when adding a new path

Issue I am facing a challenge with pushing a new path to the URI while maintaining existing search queries. For example: Current URL: https://example.com/foo?bar=123&foobar=123 When I use history.push('newPath'), I end up with https://exa ...

TypeScript combined with Vue 3: Uncaught ReferenceError - variable has not been declared

At the start of my <script>, I define a variable with type any. Later on, within the same script, I reference this variable in one of my methods. Strangely, although my IDE does not raise any complaints, a runtime error occurs in my console: Referenc ...