Angular HTML表单 - 值属性在输入字段中不起作用
中有一个模板驱动的表单
<div class="container" *ngIf="employee">
<h3>Employee {{employee.lastname}}</h3>
<form #f="ngForm" (ngSubmit)="submit(f)">
<div class="form-group">
<label for="firstname">Name </label><br>
<input type="text" class="form-control" name="firstname" ngModel value="{{employee.firstname}}" >
</div>
<button class="btn" type="submit">Update Employee Name</button>
</form>
表格的目标是更新给定员工的名称。
我在MyFormComponent模板
import { Component, OnInit } from '@angular/core';
import { Employee } from '../employee';
import { NgForm } from '@angular/forms'
import { FormsModule } from '@angular/forms';
@Component({
selector: 'app-myform',
templateUrl: './myform.component.html',
styleUrls: ['./myform.component.css']
})
export class MyFormComponent implements OnInit {
public myvar = "hi";
constructor() {}
employee?: Employee;
ngOnInit(): void {
this.getEmployee();
}
submit(f: any){
//Everything here works correctly. I do get the form object
}
getEmployee(): void {
//Everything here works correctly
}
}
。 它是空白/空的。
如果我使用“占位符”而不是“价值”,则可以使用。但是我希望它是一个默认值,因此如果用户不想更改它,则不必输入。
如果我使用.TS文件中公开声明的'myvar'而不是{{lightee.firstname}}},它也没有显示。
formsModule是在根模块中导入的。
@Angular/CLI 14.0.2。 Mozilla/Chrome
I have a template-driven form in MyFormComponent template
<div class="container" *ngIf="employee">
<h3>Employee {{employee.lastname}}</h3>
<form #f="ngForm" (ngSubmit)="submit(f)">
<div class="form-group">
<label for="firstname">Name </label><br>
<input type="text" class="form-control" name="firstname" ngModel value="{{employee.firstname}}" >
</div>
<button class="btn" type="submit">Update Employee Name</button>
</form>
The goal of the form is to update the name of a given employee.\
And the .ts file
import { Component, OnInit } from '@angular/core';
import { Employee } from '../employee';
import { NgForm } from '@angular/forms'
import { FormsModule } from '@angular/forms';
@Component({
selector: 'app-myform',
templateUrl: './myform.component.html',
styleUrls: ['./myform.component.css']
})
export class MyFormComponent implements OnInit {
public myvar = "hi";
constructor() {}
employee?: Employee;
ngOnInit(): void {
this.getEmployee();
}
submit(f: any){
//Everything here works correctly. I do get the form object
}
getEmployee(): void {
//Everything here works correctly
}
}
My problem is that the 'value' field is not showing in the app. It's blank/empty.
If instead of 'value' I use 'placeholder', it works. But I want it to be a default value there, so that the user doesn't have to type in if he does not want to change it.
If instead of {{employee.firstname}} I use 'myvar' which I declare public in the .ts file, it also does not show.
FormsModule is imported in root module.
@angular/cli 14.0.2. Mozilla/Chrome
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试使用给定的2种方法。
1。
工作演示
You can try with given 2 approach.
1.
Working Demo
我的建议是删除 * ngif =“雇员” 在顶级div中,使用 value =“ {{雇员?.firstname}}” 而不是。
所以:
My suggestion is delete *ngIf="employee" in the top level div and use value="{{employee?.firstname}}" instead.
So: