我在Angular中的表单中的输入没有显示默认值
我有一个表格,我希望在其中一个输入中具有默认值,默认值是用户的显示名称,但是它没有显示,甚至在添加为默认值的默认值时也不会显示“ 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不应使用
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 fileowner: [''] ,
withowner: this.authService.userData
or evenowner: 'hi'
just to make sure it works.