ts1109: An error occurred as there was an expectation for an angular

I am encountering an error while creating a simple form with Angular using a reactive form. I'm puzzled as to why it's indicating that something is missing:

Although I have created forms numerous times before, this is the first instance of such an error.

https://i.stack.imgur.com/Vnyj0.jpg

ts.file

  productForm: any = FormGroup;

  constructor(private fb: FormBuilder,
    public dialogRef: MatDialogRef<DialogFormComponent>, 
    @Inject(MAT_DIALOG_DATA) public data:any) { }

  ngOnInit() {
    this.initDialogForm();
  }

  initDialogForm() {
    this.productForm = this.fb.group({
      id: [this.data?.id],
      name: [this.data?.name , Validators.required],
      price: [this.data?.price , Validators.required],
      comment: [this.data?.comment , Validators.required],
      date : [this.data?.date]
    });
  }

  onSubmit() {
    this.dialogRef.close(this.productForm.value);
  }

}

html

mat-dialog-content>
    <form [formGroup]="productForm" (ngSubmit)="onSubmit()">
        <div class="from-group">
            <label for="name">Nom</label>
            <input id="name" type="text" formControlName="name" class="form-control">
        </div>
        <div class="from-group">
            <label for="price">Prix</label>
            <input id="price" type="number" formControlName="price" class="form-control">
        </div>
        <div class="from-group">
            <label for="comment">Commentaire</label>
            <input id="comment" type="text" formControlName="comment" class="form-control">
        </div>
        <div class="from-group">
            <label for="expiration">date de péremption</label>
            <input id="expiration" type="date" formControlName="date" class="form-control">
        </div>
        <mat-dialog-actions>
            <button>Valider</button>
        </mat-dialog-actions>
    </form>
</mat-dialog-content>

Answer №1

? signifies the conditional operator used inside formgroup, which is why the compiler perceives that a missing condition exists after :

   In this example, we see how 'this.productForm' is assigned values using FormBuilder in Angular:
   
   this.productForm = this.fb.group({
      id: [this.data.id],
      name: [this.data.name , [Validators.required]],
      price: [this.data.price , [Validators.required]],
      comment: [this.data.comment , [Validators.required]],
      date : [this.data.date]
    });

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

Restricting the type of user input in HTML forms

Requirements: Input must be a whole number between 2 and 99, inclusive. Current Solution: <input required type="number" min="2" max="99" oninput="this.value = Math.abs(this.value)" maxLength="2" matInp ...

A data type that exclusively accepts values from an enumerated list without mandating the inclusion of every possible value within the enum

Here's a code snippet I'm working with: enum Foo { a, b, c } type Bar = { [key in keyof typeof Foo]: string; } const test: Bar = { a: 'a', b: 'b' }; I'm encountering an issue where the code is complaining ...

Tips on downloading a dynamically created XML file using ServiceStack and Angular

Although the code below is theoretically functional, it lacks proper error handling. The issue arises when the XML file starts downloading upon opening a new window with the URL generated by the service stack. In case of a server-side error, you are left o ...

The dropdown menus in Bootstrap are expanding outside the screen when opened

When I click on the dropdown in the Navbar on the right side, the menus open up far to the right and are not visible until I scroll horizontally. Here is the code snippet: <nav class="navbar navbar-expand-lg navbar-light bg-light"> < ...

Learning to display an error message by clicking the send button in an Angular 2 application

Despite my best efforts, I have struggled to find a solution. I have searched various websites, but everywhere they only show validations on touch or hide the button until the form is valid. How can I implement validations by simply clicking a button? Here ...

Encountering problems with createMediaElementSource in TypeScript/React when using the Web Audio API

Currently, I am following a Web Audio API tutorial from MDN, but with a twist - I am using TypeScript and React instead of vanilla JavaScript. In my React project created with create-react-app, I am utilizing the useRef hook to reference the audio element ...

The ngOnChanges method does not fire when the input value changes

Here is an example of my Angular component: @Component({ selector: 'request-Component' templateUrls: './request.component.html' styleUrls: ['./request.component.scss'] }) export class ChildComponent implements OnIn ...

Angular Material input field failing to display icon

I am facing an issue with displaying an icon within an input field using Angular Material. I have tried adding the mat-icon component with the matSuffix attribute, but the icon is not showing up. Here is the code snippet I used: Link to code If anyone ca ...

Optionalize keys containing a specific character in their name

Imagine I have an object similar to the following: const obj = { a: 1, "b?": 2, "c?": 3 } The current type of this object is as follows: type Obj = { a: number; "b?": number; "c?": number; } Is there a ...

Ways to indicate a checkbox as selected within angular version 4

I'm a complete beginner in Angular 2 and I need to be able to check checkboxes when a button is clicked. I have multiple checkboxes generated in a loop like this: <tr *ngFor="let roleObj of roleNameList"> <td> <input ty ...

Position your content on the right side of the Angular Material tabs

I'm struggling with positioning content next to the tabs on the right side. I attempted placing content in a disabled mat-tab, but I don't want the content (located on the right) to be disabled. In addition, the content includes a dropdown menu ...

Basic HTML Audio Player Featuring Several Customizable Variables

I have a unique API that manages music playback. Instead of playing audio in the browser, it is done through a Discord bot. Achievement Goal https://i.stack.imgur.com/w3WUJ.png Parameters: current: indicates the current position of the track (e.g. 2:3 ...

Ways to activate a function when the active tab in mat-tab is clicked

I am currently using angular 6 and I have set up a tab system with 3 tabs, each calling a different component. <mat-tab label="one"> <score-football ></ score-football > </mat-tab> <mat-tab label="second"> <score-hockey & ...

Enclose the type definition for a function from a third-party library

I prefer to utilize Typescript for ensuring immutability in my code. Unfortunately, many libraries do not type their exported function parameters as Readonly or DeepReadonly, even if they are not meant to be mutated. This commonly causes issues because a ...

Typescript Server Problem: Critical Error - Mark-compacts Inefficiently Close to Heap Limit, Allocation Unsuccessful - JavaScript Heap Exhausted

Whenever I run my CRA project, I consistently encounter this error in my console. It seems to be related to the typescript server. Is there a solution for this issue? 99% done plugins webpack-hot-middlewarewebpack built preview 7c330f0bfd3e44c3a97b in 64 ...

Model Mongoose TypeScript Interface Type

I am working with 2 models in my project import {model, Schema, Types} from 'mongoose' interface IResource { user : Types.ObjectId | IUsers, type : Types.ObjectId | IResourceData, value : number, lastUpdate : number | Date, ...

When attempting to update to Angular Material 8 using ng update, I ended up with @angular/* version ~9.0.0-next-0. Can anyone explain why there is this

While attempting to upgrade my Angular 7 application to Angular 8 using the instructions provided here, I encountered an issue with the last step: ng update @angular/material After running this command, the Angular Material packages were successfully u ...

Display a list of records retrieved from a Firebase query using ngFor to iterate through each instance

I'm currently using firebase and angular to work on a billing list project. The list contains data showing information for a specific month, you can refer to the imagehttps://i.stack.imgur.com/ZR4BE.png While querying the list was smooth, I encounte ...

The extensive magnetic scrolling functionality in Ionic 2 sets it apart from other frameworks

Hi everyone, I could really use some assistance! I've been working on developing an Ionic 2 App and my navigation setup is not too complex. I have a main menu where clicking on an item opens another menu with a submenu. From there, if I click on an i ...

What is preventing me from setting the User object to null in my Angular application?

Currently, I am working on a project in Angular and encountering a specific issue. In my service class, the structure looks like this: export class AuthService { authchange: new Subject<boolean>(); private user: User; registerUser(authD ...