【问题标题】:binding the date object to date input's default value. but not working将日期对象绑定到日期输入的默认值。但不工作
【发布时间】:2017-10-05 10:12:19
【问题描述】:

我正在尝试通过属性绑定设置日期类型输入的默认值。

首先我在 app.component.ts 中创建了一个新的日期对象,然后将日期的 [value] 属性绑定到 app.component.ts 中的 currentDate 属性。但它不起作用

// 表单模板

<section class="container">
  <div class="panel panel-default">
    <div class="panel-heading">Add a Task</div>
    <div class="panel-body">
      <form class="form-horizontal" [formGroup]="taskForm" (ngSubmit)="onSubmit()">
        <div class="form-group">
          <label for="title" class="col-sm-2 control-label">Title *</label>
          <div class="col-sm-10">
            <input type="text" class="form-control" id="taskTitle" placeholder="Title of Task" formControlName="taskTitle">
          </div>
        </div>
        <div class="form-group">
          <label for="description" class="col-sm-2 control-label">Description *</label>
          <div class="col-sm-10">
            <input type="text" class="form-control" id="description" placeholder="Enter Your Description" formControlName="description">
          </div>
        </div>
        <div class="form-group">
          <label for="date" class="col-sm-2 control-label">Date of Completion *</label>
          <div class="col-sm-10">
            <input type="date" class="form-control" id="date" formControlName="date" [value]="currentDate">
          </div>
        </div>
        <div class="form-group">
          <div class="col-md-offset-6">
            <button type="submit" class="btn btn-default">Submit your data</button>
          </div>
        </div>
      </form>
    </div>
  </div>
</section>
<section class="container">
  <app-task-list></app-task-list>
</section>

// 应用组件

import { Component, OnInit } from '@angular/core';
import { FormGroup, FormControl } from '@angular/forms';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  currentDate: {};
  taskForm: FormGroup;

  ngOnInit() {
    this.taskForm = new FormGroup({
      'taskTitle': new FormControl(''),
      'description': new FormControl(''),
      'date': new FormControl(null)
    });
    this.currentDate = new Date();
    console.log(this.currentDate);
  }

  onSubmit() {

  }
}

【问题讨论】:

  • 尝试使用 [ngModel] 而不是 [value],例如管道:[ngModel] ="currentDate | date:'yyyy-MM-dd'"
  • 我不能使用管道。它必须在没有管道的情况下完成
  • @Laurens 你能写一个答案吗,请解释一下代码
  • 添加了代码,为什么不能使用管道?

标签: angular typescript ecmascript-6 forms angular-forms


【解决方案1】:

如果日期不是表单/输入期望的格式,它不会显示日期。您可以在组件中转换日期或使用管道。

带管道:

<form>
  <input 
      type="date" class="form-control" 
      name="currentDate" [ngModel]="currentDate | date:'yyyy-MM-dd'">
</form>

没有管道

组件:

currentStringDate;
constructor() {
    this.currentStringDate = new Date().toISOString().substring(0, 10);
}

HTML:

<input type="date" class="form-control" name="currentStringDate" [ngModel]="currentStringDate">

编辑:别忘了在你的html中使用name标签,它需要和ngModel变量同名

【讨论】:

  • 为什么使用 [value] 而不是 [ngModel] 不是一个好主意
  • 如果有效,则有效,但请查看此主题和 Gunther 的回复:stackoverflow.com/questions/42699705/ngmodel-vs-value
  • 如果我需要保存选定的日期,我需要使用两种方式绑定,但使用[(ndModel)] 时会显示expected identifier or keyword at the end of the expression [selectedDate | date:'yyyy-MM-dd'=$event]?
猜你喜欢
  • 1970-01-01
  • 2016-11-05
  • 2018-04-01
  • 1970-01-01
  • 2020-03-25
  • 2021-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多