Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

My input inside a form in Angular is not showing it's default value

I have a form in which I would like to have a default value inside one of the input, that default value being the user’s display name, however, it’s not showing, and even "hi" is not shown when added as the default value of the input, I’m not sure what it is I should do :

<form [formGroup]= "postForm" (ngSubmit)="onSubmit()" novalidate>
  <div class="mb-3">
    <label>Title</label>
    <input type="text" formControlName="title" class="form-control" required>
  </div>

  <div class="mb-3">
    <label>Content</label>
    <input type="text" formControlName="message" class="form-control" required>
  </div>

  <div class="mb-3" *ngIf="authService.userData as user" >
    <input type="text" value="{{user.displayName}}" formControlName="owner" class="form-control">
  </div>

  <div class="mb-3">
    <button type="submit" class="btn btn-primary" [disabled]="!postForm.valid">Create</button>
  </div>


</form>

And this is the typescript component related to it :

    import { Component, OnInit } from '@angular/core';
import {AuthService} from "../shared/services/auth.service";
import { PostService } from '../post.service';
import { FormBuilder, FormGroup } from '@angular/forms';
import { Router } from "@angular/router";



@Component({
  selector: 'app-create-post',
  templateUrl: './create-post.component.html',
  styleUrls: ['./create-post.component.scss']
})
export class CreatePostComponent implements OnInit {
  public postForm: FormGroup;

  constructor(public postService: PostService,
              public formBuilder: FormBuilder,
              public router: Router,public authService: AuthService){
    this.postForm = this.formBuilder.group({
      title: [''],
      message: [''],
      owner: [''] ,
      date: new Date().toLocaleString()
    })
  }

  ngOnInit(): void {
  }

  onSubmit() {
    this.postService.createPost(this.postForm.value);
    this.router.navigate(['list-posts']);
  };

}

Help is very needed and appreciated, I would also like to point out that I have tried to directly put the diplayname value in the TypeScript file but it doesnt exist there, its only available from the html file.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You shouldn’t use value with Angular controlled forms, instead, the initial value is the one you provide in the typescript file, so, please remove the value part from the html, and replace in the typescript file owner: [''] , with owner: this.authService.userData or even owner: 'hi' just to make sure it works.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading