CreateComponent receives an empty result from ngModel

I am having trouble capturing the result of the Employee object. I have defined an Employee object and tried to capture its values using [ngModel], but I am getting empty results. Can someone please assist me in resolving this issue?

Employee.ts

import {IEmployee} from "../_interfaces/IEmployee";
import {ISalary} from "../_interfaces/ISalary";
import {IDesignation} from "../_interfaces/IDesignation";

export class Employee implements IEmployee {
  public birth_date: string;
  public designations: IDesignation;
  public emp_no: string;
  public first_name: string;
  public gender: string;
  public hire_date: string;
  public id: string;
  public last_name: string;
  public salaries: ISalary;

  constructor(
    birth_date: string,
    designations: IDesignation,
    emp_no: string,
    first_name: string,
    gender: string,
    hire_date: string,
    id: string,
    last_name: string,
    salaries: ISalary,
  ) {
    this.birth_date = birth_date;
    this.designations = designations;
    this.emp_no = emp_no;
    this.first_name = first_name;
    this.gender = gender;
    this.hire_date = hire_date;
    this.id = id;
    this.last_name = last_name;
    this.salaries = salaries;
  }


}

create.component.ts

export class CreateComponent implements OnInit {


  public employee: Employee= {
    birth_date: "",
    designations: <Designations>{
      id: '',
      title: '',
      emp_no: '',
      from_date: '',
      to_date: '',
    },
    emp_no: "",
    first_name: "",
    gender: "",
    hire_date: "",
    id: "",
    last_name: "",
    salaries: <Salary>{
      id: "",
      emp_no: "",
      salary: "",
      rom_date: "",
      to_date: "",
    },
  };

  constructor() {
  }

  ngOnInit(): void {
  }

  getValues(){
    console.log(this.employee);
  }


}

create.component.html

<label>First Name</label>
<input [ngModel]="employee.first_name"  [ngModelOptions]="{standalone:true}" class="form-control" type="text">
<button class="btn btn-primary" (click)="getValues()">Save Entries</button>

Output

NULL

Answer №1

Instead of using [ngModel], opt for [(ngModel)] to achieve two-way binding.

Learn more about NgModel here

When you employ one-way binding with [] syntax to ngModel, any changes made to the domain model's value in the component class are reflected in the view. On the other hand, utilizing two-way binding with [()] syntax (also known as 'banana-in-a-box syntax'), ensures that the value in the UI remains synchronized with the domain model in your class.

<input [(ngModel)]="employee.first_name"  [ngModelOptions]="{standalone:true}" class="form-control" type="text">

Check out a sample demo on StackBlitz here

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

Expanding the default Stackblitz App component with additional standalone Angular components?

I have recently developed a separate AppNavComponent component on Stackblitz platform. import { Component } from '@angular/core'; import { CommonModule } from '@angular/common'; import { RouterOutlet } from '@angular/router'; ...

The imagemin code runs successfully, however, there appears to be no noticeable impact

I've been attempting to compress an image in Node 14 using Typescript and the imagemin library. After following some online examples, I executed the code but it seems that nothing is happening as there are no errors reported upon completion. The outp ...

Subdomain causing issues with Angular routes when page is refreshed

I have a project built with Angular. I uploaded it to a subdomain on my server: http://example.com/myapp/ However, when I try to refresh the angular routes, I encounter a 404 error. For instance, if I refresh the following URL, it returns a 404 error! htt ...

Issue - The NgFor directive is designed to only bind to Iterables like Arrays

I am attempting to showcase an array as options in a dropdown menu, but I keep encountering the following error: ERROR Error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to It ...

The functionality of the Angular application is not compatible with Edge browser

Encountering an issue with loading my dashboard on Edge (works fine on Chrome). The page fails to load, unlike in Chrome. The problem seems to be linked to the code snippet ColorScale.js.pre-build-optimizer.js: /** * Set up color sca ...

Issue with React not displaying JSX when onClick Button is triggered

I've recently started learning React and I'm facing a problem that I can't seem to figure out. I have a basic button, and when it's clicked, I want to add another text or HTML element. While the console log statement is working fine, th ...

Upgrading to TypeScript: How to resolve issues with const declarations causing errors in JavaScript style code?

Currently, I am in the process of migrating a decently sized project from JavaScript to TypeScript. My strategy involves renaming the .js files to .ts files, starting with smaller examples before tackling the larger codebase. The existing JavaScript code i ...

Include a tab button within a vertical tab list using Angular Material

I have utilized Angular Material to create a vertical tab, and I would like to incorporate an Add Tab button within the tab listing itself. Currently, when I add the button, it appears at the bottom of the layout instead. For reference, you can access the ...

Issue encountered while retrieving data from a json server using Angular (HttpErrorResponse)

I've been working on creating a task app for practice, and my goal is to retrieve all the task data from a JSON server. However, I'm encountering an issue where the data doesn't display in the browser console as expected, and instead, an err ...

modify the background color at core.scss line 149

I am struggling to change the background color of my Ionic login page. I have tried adding custom CSS in various places, such as core.scss:149, but it only works when I add it directly in Chrome developer tools. How can I get this custom background color t ...

The Typing of Mongoose 5.11.11 Schema Definitions

Recently, there was an update in Mongoose that allows it to accept a model generic. While it works perfectly fine with a string type, it seems to have trouble with a boolean type, giving the error message Type 'boolean' is not assignable to type ...

Specify a maximum length for numerical input and permit the use of a plus sign

I am currently working on a form field where users can input a country code. I want to ensure that the user enters only 3 numbers with a + sign at the beginning. I have successfully limited the length to 3 digits, but I am facing an issue with typing the ...

What is the best approach to connecting Angular 2 with Asp.net Core Api?

Just beginning my journey with ASP.net Core and Angular 2. Successfully set up my Angular 2 project, but struggling to access my WebApi. namespace Angular2Application8.Controllers { [Produces("application/json")] [Route("api/Hello")] publ ...

Iterate through the complex array of nested objects and modify the values according to specified conditions

I am currently iterating through an array of objects and then delving into a deeply nested array of objects to search for a specific ID. Once the ID is found, I need to update the status to a particular value and return the entire updated array. Issue: Th ...

What is the process for configuring ng e2e to run on a specific port?

Ever since moving to Angular 6, I've noticed that ng e2e is no longer able to run on an available port like it used to. In the past, when we were using Angular 4, the app service would be on port 4200 and running ng e2e would automatically select a fr ...

Having trouble centering an icon in a cell within AG Grid?

I am struggling with aligning my checkmarks within the cells of my AG Grid. Currently, they are all left aligned but I need them to be centered. Adjusting the alignment for text is not an issue, but I can't seem to center the material icons. It appear ...

The attribute 'fromHTML' is not recognized within the structure of 'jsPDF' object

When attempting to convert an HTML code to a PDF using jsPDF, I encountered an issue. Despite trying to use the recommended jsPDF.fromHTML() function, I received an error message stating: "Property 'fromHTML' does not exist on type 'jsPDF&ap ...

Verifying the eligibility of a value for a union type

In my app, there is a type called PiiType that enforces strong typing: type PiiType = 'name' | 'address' | 'email'; When receiving potentially sensitive information from the server, we need to verify if it aligns with this d ...

TypeScript: Unable to fetch the property type from a different type

Currently, I'm using create-react-app with its TypeScript template. However, I encountered an issue while attempting to retrieve the type of the property 'pending' in 'GenericAsyncThunk', similar to how it's done in the redux- ...

What is the best way to rekindle the d3 force simulation within React's StrictMode?

Creating an interactive force directed graph in React using D3 has been successful except for the dragging functionality not working in React StrictMode. The issue seems to be related to mounting and remounting components in ReactStrict mode 18, but pinpoi ...