【发布时间】: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