Angular DateModel停止处理添加名称attribut

时间:2017-12-11 15:47:07

标签: javascript angular mean

我想让一个dateField在用户输入路线时被填充。所以这是我使用的代码:

formular1.component.ts

constructor(
    private validateService: ValidateService,
    private flashMessage: FlashMessagesService,
    private authService: AuthService,
    private router: Router,
    private terminalService: TerminalService
  ) {
    this.todaysDate = new Date();
  }

formular1.component.html

<input type="date" [ngModel]="todaysDate | date:'dd-MM-yyyy'" (ngModelChange)="todaysDate = $event" [value]="todaysDate | date:'yyyy-MM-dd'">

这很好用,dateField填充了正确的值,但我的控制台内部出现了很大的错误:

Error: If ngModel is used within a form tag, either the name attribute must be set or the form
      control must be defined as 'standalone' in ngModelOptions.

      Example 1: <input [(ngModel)]="person.firstName" name="first">
      Example 2: <input [(ngModel)]="person.firstName" [ngModelOptions]="{standalone: true}">

听起来很简单......但是当我添加name="test"属性时,dateField不再有效并且没有填充日期值 - 为什么?

也许有人也可以告诉我,如何在德国获得正确的时间(夏季/冬季)我们实际上距离New Date();

的+1小时

1 个答案:

答案 0 :(得分:1)

问题在于日期格式化管道

  <input type="date" name="todayDate" id="todayDate" [ngModel]="todaysDate | date:'yyyy-MM-dd'" (ngModelChange)="todaysDate = $event" [value]="todaysDate | date:'yyyy-MM-dd'">

如果您想转换为dd-mm-yyyy格式check this one