Angular HTML表单 - 值属性在输入字段中不起作用

发布于 2025-02-11 11:02:16 字数 1408 浏览 2 评论 0原文

中有一个模板驱动的表单

<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 技术交流群。

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

发布评论

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

评论(2

内心旳酸楚 2025-02-18 11:02:16

您可以尝试使用给定的2种方法。

1。

<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)]="employee.firstname"
    />
  </div>
  <button class="btn" type="submit">Update Employee Name</button>
</form>
<form #f="ngForm" (ngSubmit)="submit(f)">
  <div class="form-group">
    <label for="firstname">Name </label><br />
    <input
      type="text"
      class="form-control"
      name="firstname"
      [value]="employee.firstname"
    />
  </div>
  <button class="btn" type="submit">Update Employee Name</button>
</form>

工作演示

You can try with given 2 approach.

1.

<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)]="employee.firstname"
    />
  </div>
  <button class="btn" type="submit">Update Employee Name</button>
</form>
<form #f="ngForm" (ngSubmit)="submit(f)">
  <div class="form-group">
    <label for="firstname">Name </label><br />
    <input
      type="text"
      class="form-control"
      name="firstname"
      [value]="employee.firstname"
    />
  </div>
  <button class="btn" type="submit">Update Employee Name</button>
</form>

Working Demo

ㄟ。诗瑗 2025-02-18 11:02:16

我的建议是删除 * ngif =“雇员” 在顶级div中,使用 value =“ {{雇员?.firstname}}” 而不是。

所以:

<div class="container">
  <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>
 .
 .

My suggestion is delete *ngIf="employee" in the top level div and use value="{{employee?.firstname}}" instead.

So:

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