2

I'm trying to use the ng2-datepicker for choosing a date and afterwards displaying it on a table. I'm having the issue that the binding to my chosen date doesn't work. I guess that it has to do something with the format option that I made (DD.MM.YYYY). In my html, I have this code:

<ng2-datepicker [(ngModel)]="date" [options]="datepickeroptions"></ng2-datepicker>

datepickeroptions with typescript is like this:

this.datepickeroptions = {
  format: "DD.MM.YYYY",
  locale: "de",
};

I tried to initialize the date variable as Date and as string, but none of them worked. I get just [object Object] as value from ngModel although the value in the input container looks ok. Thanks in advance for your help.

object Object in parent container

4

2 回答 2

1

最后,我找到了解决方案!起初我发现 json 管道给了我正确的属性。然后我在我的任何类型的 .ts 文件上创建了一个截止日期输入,并在 OnInit 中像这样初始化它:

 this.deadlineinput = {
        formatted: ''
    };

在这里,还可以添加他想要访问的其他选项(json 和 html 看起来像这样:

{ "day": "05", "month": "01", "year": "2017", "formatted": "05.01.2017", "momentObj": "2017-01-04T23:00:00.000Z" }

我把它放在.hmtl上:

<ng2-datepicker [(ngModel)]="deadlineinput" [options]="datepickeroptions"></ng2-datepicker> <label> selected date is: {{deadlineinput.formatted}} </label>

我知道它不应该是这样的,并且我在 github 上为当前版本打开了一个问题,但至少它是一种对我现在有帮助的解决方法。

于 2017-01-21T09:50:16.923 回答
0

您应该使用香蕉盒表示法来仅使用框:

[(ngModel)]="date"
  • ():从视图到控件数据绑定
  • []:从控件到视图数据绑定
  • [()]: 2 路数据绑定
于 2017-01-20T17:06:26.227 回答