【问题标题】:INPUT DATE VALUE ANGULAR输入日期值角度
【发布时间】:2022-01-31 03:03:08
【问题描述】:

SERVICE.TS

addP(nome: string, cognome: string, anno_n: string): Observable<any> {
     return this.http.post<Partecipanti>(this.partecipantiUrl, {
       nome: nome,
       cognome: cognome,
       anno_n: anno_n 
     }, this.httpOptions).pipe(
       tap((newPartecipante: Partecipanti) => this.log(`partecipante aggiunto w/ id=${newPartecipante.id}`)),
       catchError(this.handleError<Partecipanti>('addP'))
     );
   }
COMPONENT.TS

add(nome: string, cognome: string, anno_n: string): void {
      this.PartecipantiService.addP(nome, cognome, anno_n).subscribe(res => {
          console.log(res);
          this.loadUser();
        })

      }
<form class="row row-cols-lg-auto g-3 align-items-center float-center" (ngSubmit)="add(nome.value, cognome.value, anno_n.value)" (ngSubmit)="loadUser()" style="justify-content: center;">
      <div class="col-12">
        <label class="visually-hidden" for="inlineFormInputGroupUsername">Nome</label>
        <div class="input-group">
          <input type="text" class="form-control" id="inlineFormInputGroupUsername" required #nome placeholder="Nome">
        </div>
      </div>
      <div class="col-12">
        <label class="visually-hidden" for="inlineFormInputGroupUsername">Cognome</label>
        <div class="input-group">
          <input type="text" class="form-control" id="inlineFormInputGroupUsername" required #cognome placeholder="Cognome">
        </div>
      </div>
      <div class="col-12">
        <label class="visually-hidden" for="inlineFormInputGroupUsername">Data di nascita</label>
        <div class="input-group">
          <input type="text" class="form-control" id="inlineFormInputGroupUsername" required #anno_n useValueAsDate placeholder="Data di nascita (GG/MM/AAAA)">
        </div>
      </div>
    
      <div class="col-12">
        <button type="submit" class="btn btn-primary shadow-lg">Salva</button>
      </div>
    </form>

如何在“添加”函数中获取日期值? 在我的函数添加中,我有 3 个字段(姓名、姓氏和日期),当我调用我的函数添加按钮时,我使用 (ngSubmit)="add(name.value, surname.value, date...?)。什么我必须使用?值是字符串,我找不到日期的东西!这部分在 component.html 上你能解释一下它是如何工作的吗?我的输入是 type="date"

【问题讨论】:

  • 请格式化您的问题。阅读How to Askeditminimal reproducible example
  • user.ts { name: string, surname: string,birth: Date} service.ts add(name: string, surname: string,birth: Date){ code} user.html
  • 我不知道如何在 html 中的函数中获取日期值

标签: html angular


【解决方案1】:

我建议你使用 ngForm。这是处理这种情况的最简单方法。

在此处查看文档:https://angular.io/api/forms/NgForm

这是我做的一个工作示例:https://stackblitz.com/edit/angular-ivy-pqgewm?devtoolsheight=33&file=src/app/app.component.html

本质上你希望你的 HTML 看起来像这样:

<form #form="ngForm" (ngSubmit)="userService.submitForm(form)">
  <div>
    <label for="firstName">First Name</label>
    <input name="firstName" id="firstName" type="text" ngModel required />
  </div>
  <div>
    <label for="lastName">Last Name</label>
    <input name="lastName" id="lastName" ngModel type="text" required />
  </div>
  <div>
    <label for="dateOfBirth">Date of Birth</label>
    <input name="dateOfBirth" id="dateOfBirth" ngModel type="date" required />
  </div>
  <br />
  <button type="submit">Submit</button>
</form>

你的提交函数看起来像这样:

submitForm(form: NgForm) {
    if (form.valid) {
      const user: User = {
        name: form.value['firstName'],
        surname: form.value['lastName'],
        birth: new Date(form.value['dateOfBirth']),
      };
    }
  }

【讨论】:

  • Ty 男人,但它不起作用!
  • 什么不起作用?我检查了 Stackblitz 链接,这一切似乎都有效。 link我需要更多关于你期望什么以及什么不起作用的详细信息。
  • 我的控制台不显示日期,因为我的模块有问题
猜你喜欢
  • 2020-02-09
  • 2022-01-26
  • 2014-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-17
  • 1970-01-01
  • 2015-01-14
相关资源
最近更新 更多