我在Angular中的表单中的输入没有显示默认值

发布于 2025-02-04 10:54:07 字数 1946 浏览 4 评论 0原文

我有一个表格,我希望在其中一个输入中具有默认值,默认值是用户的显示名称,但是它没有显示,甚至在添加为默认值的默认值时也不会显示“ hi”输入,我不确定我应该做什么:

<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>

这是与之相关的打字稿组件:

    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']);
  };

}

帮助非常需要和赞赏,我还想指出,我试图将Diellayname值直接放入TypeScript文件,但不存在该文件,仅可从HTML文件可用。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦情居士 2025-02-11 10:54:07

您不应使用value带有角控制表单,而是初始值是您在打字稿文件中提供的值,因此,请从HTML中删除值部分,然后替换在打字稿文件中所有者:[''],带有所有者:this.authservice.userdata,甚至所有者:'hi''只是为了确保其有效。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文