Navigate through Angular 2 array elements

I am facing an issue where I have an array and I need to display only a single object from the array at a time. The goal is to cycle through the array using a button, but I'm struggling to figure out how to display just one object at a time. You can see what I have so far on this plunker.

I'm unsure if I am correctly utilizing *ngFor in this context. Any help would be greatly appreciated!

Answer №1

@Component({
  selector: 'my-app',
  template: `
    <div>
      <div>{{index}}</div>
      <h2>{{item[index].title}}</h2>
      <button (click)="Next()">next</button>
    </div>
  `,
})
export class App {
  public itemsList = ITEM;
  constructor() {}
  index = 0;

  MoveToNext(id){
    this.index ++;
    if(this.index >= this.itemsList.length) {
      this.index = 0;
    }
  }
}

Plunker example

Answer №2

@Component({
  selector: 'my-app',
  template: `
    <div>
      <h2 *ngFor="let individualItems of items">{{individualItems.title}}</h2>
      {{counter}}
      <button (click)="advance()">proceed</button>
    </div>
  `,
})
export class Application {
  public segment = SEGMENT;
  public counter = 0;

  public get items() {
    return this.segment.filter((item, counter) => index <= this.counter);
  }
  constructor() {}

  public advance(){
   this.counter = Math.min(++this.counter, this.segment.length - 1);
  }
}

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

Guide to properly defining the interface for JSON data in Angular6

component.ts import * as AssetId from "../../../assetid.json"; export class AssetID { abc: number; xxx: number; } export class Sample { public assetId: AssetID = AssetId; // Line no : 9 } assetid.json { "abc": 3, "xxx": 4 } Error ...

Struggling with aligning mat-icon in the center using HTML and CSS?

My issue is that the mat-icon in my generated columns is not center aligned. What could be causing this? When using ngFor to generate my datatable columns dynamically, none of them align correctly. The mat-icon inside my DIV defaults to left alignment. ...

Challenge with the scope of 'this' in Typescript

Whenever I invoke the findFromList function from a component, it triggers this particular error message: ERROR TypeError: Cannot read property 'searchInArray' of undefined at push../src/app/shared/services/filter.service.ts.FilterService ...

Assigning fields dynamically based on a generic string union concept

My goal is to create a function that can dynamically add fields and functions to an object based on arguments provided. However, I'm encountering an issue where the function does not recognize the types of these dynamic fields. Here's a simple ex ...

Angular tips: Retrieving user ID for API authorization

Within the authorize.service.ts file, there is a service code block that allows me to retrieve the current logged in user's userName: public getUser(): Observable<IUser | null> { return concat( this.userSubject.pipe(take(1), filter( ...

Navigating through child routes in Angular can be tricky, especially when dealing with unknown routes

I'm having some trouble with routing today. I have a collection of components each associated with a unique route. I am trying to include a 'PageNotFoundComponent' for any incorrect route in both the parent and child components. Let me expla ...

Leverage elements from nearby npm repository when building an Angular 2 application

After developing a generic chart component using d3 and Angular 2, I decided to share it by publishing it in a local npm repository. This way, anyone can easily incorporate the chart component into their Angular project by simply running the npm install my ...

Seeking a filterable data grid in Angular 2

Has anyone come across a Sortable Table for angular4 that is readily available? I have seen examples of ng2table and Bootstrap table being integrated into Angular4 components with custom sort implementation. I am leaning towards using Bootstrap table with ...

Typescript indicates that an object may be potentially null

I've hit a roadblock where I keep getting warnings that the objects might be null. After searching online and on StackOverflow, I've tried numerous solutions with no luck. My goal is to insert the text "test" into the HTML elements using their ID ...

What is the best way to access the rendered child components within a parent component?

I am seeking a way to retrieve only the visible child components within a parent component. Below is my unsuccessful pseudo-code attempt: parent.component.html <parent (click)="changeVisibility()"> <child *ngIf="visible1"></child> ...

Issues with TypeScript: Difficulty locating names in HTML templates

I recently upgraded my Angular 7 code to Angular 9 by following the steps outlined in the Angular Upgrade guide. However, upon completion of the migration process, I started encountering numerous "Cannot find name" errors within the HTML templates of my co ...

What is the most effective way to manage JSON Data in Angular 2?

Hello there, I am a newbie to Angular, specifically Angular 2, so please go easy on me :). I've been experimenting with using WordPress as my data API with the help of the WP API plugin. So far, I've managed to retrieve posts from WordPress. Belo ...

Element remains intact after DOM manipulation even during router navigation

One of my directives has the functionality to relocate the element it is applied to on the body of the document: @Directive({ selector: '[myDirective]' }) export class MyDirective implements AfterViewInit { constructor(private elem: ElementR ...

Tips for resolving a typescript Redux connect error related to ActionCreatorsMapObject

Encountering typescript error: ERROR in [at-loader] ./src/app/components/profile/userProfile/_userMain.tsx:130:37 TS2345: Argument of type 'typeof "/usr/src/app/src/js/src/app/redux/actions/sessionActions"' is not assignable to parameter of ...

The issue persists with react-hook-form and Material UI RadioGroup as the selected value remains null

Having trouble with RadioGroup from Material UI when using react-hook-form Controller. I keep getting a null selected value, even though my code seems right. Can you help me figure out what's missing? import * as React from "react"; import { ...

Collection of strung items that require removal of strings

I am currently working with an array of string objects and need help with formatting them. creatingTaskMatrix(pages) { let allTasks = []; for (let i = 0; i < pages.length; i++) { const page = pages[i]; for (let j = 0; j < page.tasks.length; j+ ...

What could be causing the slower performance of my Angular App on Azure App Service Linux compared to App Service Windows?

I have a unique Angular application that interfaces with .NET 6 WebAPIs. The app was built using the Visual Studio 2022 template labeled "ASP.NET Core with Angular". It makes calls to an SQL Server database located on Azure. Interestingly, when I deploy th ...

Exploring the best way to access ViewContainerRef: ViewChild vs Directive

While researching, I came across a recommendation in the Angular Docs that suggests using a directive to access the ViewContainerRef for creating dynamic components. Here is an example of such a directive: import { Directive, ViewContainerRef } from &apos ...

What are some best practices for managing object-level variables in TypeScript and Vue.js?

Uncertain about the optimal approach, I am looking to create a component and leverage some object level variables. Consider the example below: import Vue from "vue" import * as paper from "paper" export default Vue.extend({ template: ` <d ...

Exporting numerous modules from an NPM package

Currently, I am working on a large Node and Typescript project called Project A. This project consists of numerous modules that I intend to reuse in another project, Project B. In order to achieve this reusability, I have configured the tsconfig.json file ...