Leveraging bespoke components for data within Ionic 2

I have designed a custom component that functions like this:

student.ts

import { Component, Input } from '@angular/core';

@Component({
  selector: 'student',
  templateUrl: 'student.html'
})
export class StudentComponent {

  @Input() name: string;
  public status: string;
  public notes: string;

  public tardyTime: string;
  public isTardy : boolean = false;;

  constructor() {
  }

  tardyClicked(){
    this.isTardy = true;
  }

  notTardy(){
    this.isTardy = false;
  }
}

Here's the template structure:

student.html

<h4 class="padding">{{name}}</h4>
<ion-segment [(ngModel)]="status">
    <ion-segment-button value="p" (ionSelect)="notTardy()">
      Present
    </ion-segment-button>
    <ion-segment-button value="t" (ionSelect)="tardyClicked()" >
      Tardy
    </ion-segment-button>
    <ion-segment-button value="a" (ionSelect)="notTardy()">
      Absent
    </ion-segment-button>
  </ion-segment>
  <ion-item *ngIf="isTardy" >
    <ion-label>Tardy Time:</ion-label>
    <ion-datetime displayFormat="h:mm a" [(ngModel)]="tardyTime"></ion-datetime>
  </ion-item>
  <ion-item>
    <ion-input [(ngModel)]= "notes" type="text" placeholder="Notes:"></ion-input>
  </ion-item>

When implementing the custom component in my application, I do it as follows:

page.html

<ion-content padding>
  <ion-list>
    <student *ngFor="let student of studentArray" [name]="student" ></student>
  </ion-list>
  <p>
    <button ion-button color="primary" (click)="showPrompt()" block>Submit</button>
  </p>
</ion-content>

I am interested in creating an array of objects in my page.ts file that gathers information from the student components and creates an object for each, with properties such as:

{
  name : studentcomponent.name
  status : studentcomponent.status
}

Do you think this is achievable?

Answer №1

To start, establish the object array in your page.ts file with an initial status value. Next, pass the student object to the student component using the following code:

<ion-list>
    <student *ngFor="let student of studentArray"
       [student]="student"></student>
</ion-list>

As the student component alters the status, it will also reflect the changes in your page.ts file.

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

Submitting a value to ngForm: A step-by-step guide

I am working on an ngForm within Angular Material that contains several input fields. I want to include a new field called total in the submission, but this field is not an input field. It should be a readonly field and its value needs to come from the Typ ...

The error message "tsc not found after docker image build" appeared on the

My goal is to deploy this program on local host. When I manually run "npm run build-tsc," it works successfully. However, I would like Docker to automatically run this command when building the image. Unfortunately, I receive an error saying that tsc is no ...

"Elaborate" Typescript Conditional Generic Types

Scenario I am currently working on implementing strong typing for the onChange function of a UI library's Select component. To provide some context, the existing type definition for their onChange is as follows: onChange?: (...args: any[]) => v ...

The justify-content: space-between property does not work in a nested flex box scenario

I'm currently working on styling a cart in Angular and facing an issue with aligning the price all the way to the right of the cart. I attempted using `space-between` within the outer div, which worked fine, but when applied to the inner div, it doesn ...

Access the Angular application directly from the email

Our infrastructure consists of a .NET back-end, an Angular 5 application, and a nginx server. Upon registering your account in the application, you will receive an email with a verification link structured as follows: [root]/register/verify?userId=blabla& ...

Leverage TypeScript AngularJS directive's controller as well as other inherited controllers within the directive's link function

I am currently developing an AngularJS directive in TypeScript for form validation. I am trying to understand how to utilize the directive's controller and inherit the form controller within the directive's link function. Thank you in advance! ...

Learn how to use JavaScript to parse binary files

Is there a way to interpret this binary data below? Binary2 { sub_type: 0, buffer: Buffer(16) [ 12, 15, 64, 88, 174, 93, 16, 250, 162, 5, 122, 223, 16, 98, 207, 68 ], position: 16 } I've attempted different methods like using ...

Creating image paths for a list of items in reactjs can be achieved by mapping through the list

My goal is to display a list of items, with each item being assigned an image based on its ID: private static renderItemsTable(products: Product[]) { return <table className='table'> <thead> <tr> ...

The property 'x' cannot be found on the data type 'true | Point'

I am dealing with a variable named ctx which can be either of type boolean or Point. Here is how Point is defined: type Point = { x: number y: number } In my React component, I have the following setup: const App = () => { const [ctx, toggleC ...

Transfer my testing utilities from React Router version 5 to version 6

I am currently transitioning my project to React V6 router and encountering an issue with my test utility function. Every time I run the test, all my expectations fail because jest cannot locate the object. Has anyone faced this error during a similar migr ...

Put Off Observable Tasks

In the past, I utilized a standard WebSocket to send messages and await responses by employing $q.defer. This involved storing the deferred object and returning a promise. The response handler would then locate the stored deferred object and resolve it wit ...

forEach was unable to determine the appropriate data types

define function a(param: number): number; define function b(param: string): string; define function c(param: boolean): boolean; type GeneralHooks<H extends (...args: any[]) => any> = [H, Parameters<H>] const obj = { a: [a, [1]] as Gene ...

The Angular HttpErrorResponse is a class used for handling

I am encountering an issue while attempting to retrieve a user's cart by submitting the username in the request URL. list: PanieDto[] = []; @Input() listform: PanieDto = new PanieDto(); isLoggedIn = false; showAdminBoard = false; showModeratorBoard = ...

The FormData object appears to be blank, even though it was supposed to be populated when attempting to send a PDF file using a multipart FormData POST request in Cypress

I am attempting to send a PDF file as a POST request. The API supports the use of @RequestPart and @RequestParam: @RequestPart("file") MultipartFile file; @RequestParam(value = "document-types", required = false) Set<String> documentTypes; My appro ...

What are some strategies I can implement to effectively manage different errors and ensure the app does not crash

I came across a variety of solutions for error handling. The key concept revolves around this explanation: https://angular.io/api/core/ErrorHandler Attempts were made to implement it in order to capture TypeError, however, the outcome was not successful: ...

Dropdown element with PrimeNG adorned with a span

I am trying to create a simple form with text inputs and dropdowns. I have successfully implemented the textInput, but I am struggling with the dropdowns. Below is the code that I have so far: <div class="p-grid p-dir-col p-offset-2"> ...

Is there a way to retrieve the id attribute of an option element from the source component?

When a user makes a selection, I am trying to access the id attribute of the HTMLOptionElement. However, it always returns 0 instead of the actual id I passed (such as 1 or 2) from the select tag: <div class="col-8"> <select (cha ...

Differentiating Angular HTML and script code is a fundamental practice in Angular development

I am working on an angular frontend project that includes a dashboard component with a sidebar. The code currently has both the script tag and HTML code in the same file, making it difficult to manage. I am looking for a way to separate them to make the co ...

Typescript Error:TS2345: The argument '{ theme: string; jsonFile: string; output: string; }; }' is not compatible with the parameter type 'Options'

Encountering an error mentioned in the title while using the code snippet below: import * as fs from 'fs' import { mkdirp } from 'mkdirp' import * as report from 'cucumber-html-reporter' const Cucumber = require('cucumber ...

Angular - Transform calendar dates to a lively green upon initial popup activation

I'm looking to customize my calendar so that all the dates in my list have a green background when the calendar is opened. ngOnInit(): void { this.roomService.getReservableDatesFromRoom(room.roomName).subscribe(data => { for (let i = 0; i ...