What is the best way to implement ngModel globally in my application?

As a newcomer to Angular 5, I am struggling to display values from the Add screen to the View screen using ngModel. My aim is to have the values added in the Add screen appear in the View screen's table.

// In my App.module.ts file
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import {RouterModule, Routes} from '@angular/router';
import {Component} from '@angular/core';
import {NgForm} from '@angular/forms';
import {FormsModule} from '@angular/forms';

import { AppComponent } from './app.component';
import { HeaderComponent } from './header/header.component';
import { BodyComponent } from './body/body.component';
import { FooterComponent } from './footer/footer.component';
import { UomComponent } from './uom/uom.component';
import { AddComponent } from './uom/add/add.component';
import { DetailsComponent } from './uom/details/details.component';
import { DeleteComponent } from './uom/delete/delete.component';

// Rest of the code remains unchanged...

Answer №1

Give this a shot! It will display all recently added records.

HTML CODE

<input type="text" [(ngModel)] = "itemInput.itemId"></td>
<input type="text" [(ngModel)] = "itemInput.desc"></td>
<input type="text" [(ngModel)] = "itemInput.classification"></td>
<button (click)="save()"></button>
<table> 
    <thead>
        <tr height="25">
            <th width="25%">Item ID</th>
            <th width="25%">Item Description</th>
            <th width="25%">Item classification</th>
        </tr>
    </thead>
    <tbody>
    <tr *ngFor="let item of itemList ">
        <td>{{item.itemId}}</td>
        <td>{{item.desc}}</td>
        <td>{{item.classification}}</td>
    </tr>
    </tbody>
 </table>

COMPONENT CODE

export class AddComponent {
  itemInput:any = {};
  itemList = [];
  constructor() { }

  save() {
      itemList.push(itemInput);
      itemInput = {};
  }
}

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

Executing a function in the view/template with Angular 2+

Whenever a function is called in the view of an Angular component, it seems to be executed repeatedly. A typical example of this scenario can be seen below: nightclub.component.ts import { Component } from '@angular/core'; @Component({ selec ...

utilizing different types of text style

Whenever I write a paragraph, I want the text to be styled like this: Click here for alt text http://img6.imageshack.us/img6/9894/fullscreencapture282010.jpg Do I need to use CSS to achieve this kind of formatting? I'd like it to look similar. ...

Creating customizable Isotope objects using custom CSS margins that are influenced by the child-nth selector and JavaScript

My recent project involved creating a simple .isotope gallery, and when viewing the selected #portfolio-wrap container in Chrome Dev Tools, here is how it appears: Unfortunately, I am unable to upload three links here. Please visit this link for more info ...

Sending a responsive email through Outlook can cause issues with the 100% width tables

After using a boilerplate to create a responsive email that looks good across devices, there seems to be an issue with Gmail on Android. However, you can't always have everything perfect. When forwarding the email in Outlook 2007 or 2010, the table b ...

Eliminate the hashtag (#) from the URL in Angular 11

I am facing an issue with removing the # from the URL. When I try to remove it, the application encounters a problem upon deployment to the server. Upon page refresh, a 404 error status is returned. For instance: https://example.com/user/1 (works) https ...

Creating a Visual Loading Effect in HTML

My website's layout scheme gets lost if the page isn't fully loaded. I'm interested in implementing an animation that is tied to the loading progress, similar to a progress bar but without the visual element of a bar itself. One idea is to ...

How to send parameters to the jQuery delete button click event handler

Here is the jQuery code I am working with: $('#btnDelete').click( function() {//Do the delete here via jquery post}); In my table, each row has a delete button like this: <a id="btnDelete">Delete</a> I need to pass parameters to t ...

What's causing all my carousel slides to stack on top of each other instead of presenting in a

Whenever I click 'run' or 'preview' in a browser, specifically using Chrome, all three slides are displayed stacked from 1 to 3 in a static view. They do not toggle through each slide as intended. This issue only occurs on Codeply, a p ...

jQuery toggle function fails to toggle div element as intended

I am a JavaScript newbie and I am attempting to create a basic hide and show div toggle, but unfortunately, it is not functioning properly. I cannot seem to pinpoint the issue – initially, I set the div's display to none, then when the course-info-t ...

The bottom of the page refuses to remain anchored

I've exhausted all possible solutions and my footer just refuses to stay at the bottom of the page. Working with Opencart has made it challenging for me to pinpoint the root cause, leaving me utterly perplexed. The issue persists on pages with minim ...

Creating a versatile design using a combination of grids, HTML tables, and media queries to ensure

When it comes to developing a responsive website, which method is superior: utilizing grid systems, employing media queries, or relying on HTML tables? ...

Positioning a button at the center-right

I'm currently in the process of creating a coming soon website, which you can find hosted here. However, I am having some trouble aligning the button correctly. My goal is to have it positioned to the right of the text field. How can I achieve this? ...

Exploring Font Choices: Customizing Your Text Style

I've been attempting to incorporate my own font into my website, but despite researching several Stack Overflow articles, I'm facing various browser-specific and path-related issues. Sadly, I haven't been able to successfully display my font ...

What are the best ways to position elements within a div?

In my small information div, I am looking to align the content on the right side. Specifically, 065 31 323 323, <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a5d4c4c1c0cbdfc4e5c2c8c4ccc98bc6cac8">[email protected]</ ...

My Angular 6 app kept encountering this error repeatedly, eventually causing the browser to freeze

[Violation] A non-passive event listener was added to a scroll-blocking 'touchstart' event. Consider marking the event handler as 'passive' to improve page responsiveness I have been successfully using Tabulator in Angular, but after s ...

Customize your form fields with Bootstrap 3 input groups

Currently, I am working on implementing a bootstrap 3 input group, but unfortunately, the outcome is not meeting my expectations. My goal is to create a select [input]-[input text]-[button] structure all in one line. However, in my scenario, the button has ...

How can I dynamically generate multiple Reactive Forms from an array of names using ngFor in Angular?

I am in the process of developing an ID lookup form using Angular. My goal is to generate multiple formGroups within the same HTML file based on an array of values I have, all while keeping my code DRY (Don't Repeat Yourself). Each formGroup will be l ...

How come the background image is not being applied using ::before?

My goal is to have a background image for the body with content placed on top of it. I am using CSS to set the background image through the body selector with the ::before pseudo-element. However, the image does not show up in the background without settin ...

Difficulty in displaying JavaScript function output as text

I'm currently developing a program that randomly selects and prints a function from an array list. I am facing difficulties in getting the result to print correctly. Below is the snippet of code: const hiddenElements = document.querySelectorAll( &qu ...

Guide on redirecting to the login page in angular2 when a process is left incomplete

I am facing an issue with the Popup used for device verification during login. When I click on the login button with valid credentials, a popup opens if the device is not trusted. Once I provide the necessary information and successfully validate, it shoul ...