Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 带有moment.js的AngularJS表单上的默认日期问题_Javascript_Html_Angularjs_Momentjs - Fatal编程技术网

Javascript 带有moment.js的AngularJS表单上的默认日期问题

Javascript 带有moment.js的AngularJS表单上的默认日期问题,javascript,html,angularjs,momentjs,Javascript,Html,Angularjs,Momentjs,我将AngularJS与moment.js一起使用。我加载以下代码,它总是将今天设置为网页上日期选择器控件中的默认日期。我希望默认设置为NULL,是否可以?似乎是加载moment.js导致了这种情况 .config(函数($mdDateLocaleProvider){ $mdDateLocaleProvider.formatDate=函数(日期){ 返回时间(日期)。格式('DD-MM-YYYY'); }; }) 出生日期 根据ng模型,类型为日期的是必需属性。因此,不能有默认值,默认值为nu

我将AngularJS与moment.js一起使用。我加载以下代码,它总是将今天设置为网页上日期选择器控件中的默认日期。我希望默认设置为
NULL
,是否可以?似乎是加载moment.js导致了这种情况

.config(函数($mdDateLocaleProvider){
$mdDateLocaleProvider.formatDate=函数(日期){
返回时间(日期)。格式('DD-MM-YYYY');
};
})
出生日期
根据
ng模型,类型为
日期的
是必需属性。因此,不能有默认值,默认值为
null
,它必须是
日期的一种类型:

<md-datepicker ng-model="birthday"></md-datepicker>

根据
ng模型,类型为
日期的
是必需属性。因此,不能有默认值,默认值为
null
,它必须是
日期的一种类型:

<md-datepicker ng-model="birthday"></md-datepicker>


<>代码>对于任何发现上述解决方案有点不足的人,请考虑备选方案:

HTML

    <div>
        <input matInput [matDatepicker]="dp" [formControl]="date" placeholder="...">
        <mat-datepicker-toggle matSuffix [for]="dp"></mat-datepicker-toggle>
             <mat-datepicker #dp 
                                  startView="year"
                                  (yearSelected)="chosenYearHandler($event)"
                                  (monthSelected)="chosenMonthHandler($event, dp)"
                                  panelClass="example-month-picker">
             </mat-datepicker>
    </div>

棱角分明

    date = new FormControl(); //Initialize the date without moment

    chosenYearHandler(normalizedYear: Moment) {
        const ctrlValue = new FormControl(moment()).value; //Initialize moment when clicked
        ctrlValue.year(normalizedYear.year());
        this.date.setValue(ctrlValue);
    }

    chosenMonthHandler(normlizedMonth: Moment, datepicker: MatDatepicker<Moment>) {
        const ctrlValue = new FormControl(moment()).value; //Initialize moment when clicked
        ctrlValue.month(normlizedMonth.month());
        this.date.setValue(ctrlValue);
        datepicker.close();
    }
date=newformcontrol()//初始化没有时间的日期
chosenYearHandler(标准化年份:时刻){
const ctrlValue=new FormControl(矩()).value;//单击时初始化矩
ctrlValue.year(normalizedYear.year());
此.date.setValue(ctrlValue);
}
chosenMonthHandler(标准化月份:时刻,日期选择器:MatDatepicker){
const ctrlValue=new FormControl(矩()).value;//单击时初始化矩
ctrlValue.month(normalizedMonth.month());
此.date.setValue(ctrlValue);
datepicker.close();
}
只有对文档的修改才能从初始化中删除矩()。
参考文献:

< P>对于发现上述解决方案有任何不足之处的其他人,请考虑备选方案:

HTML

    <div>
        <input matInput [matDatepicker]="dp" [formControl]="date" placeholder="...">
        <mat-datepicker-toggle matSuffix [for]="dp"></mat-datepicker-toggle>
             <mat-datepicker #dp 
                                  startView="year"
                                  (yearSelected)="chosenYearHandler($event)"
                                  (monthSelected)="chosenMonthHandler($event, dp)"
                                  panelClass="example-month-picker">
             </mat-datepicker>
    </div>

棱角分明

    date = new FormControl(); //Initialize the date without moment

    chosenYearHandler(normalizedYear: Moment) {
        const ctrlValue = new FormControl(moment()).value; //Initialize moment when clicked
        ctrlValue.year(normalizedYear.year());
        this.date.setValue(ctrlValue);
    }

    chosenMonthHandler(normlizedMonth: Moment, datepicker: MatDatepicker<Moment>) {
        const ctrlValue = new FormControl(moment()).value; //Initialize moment when clicked
        ctrlValue.month(normlizedMonth.month());
        this.date.setValue(ctrlValue);
        datepicker.close();
    }
date=newformcontrol()//初始化没有时间的日期
chosenYearHandler(标准化年份:时刻){
const ctrlValue=new FormControl(矩()).value;//单击时初始化矩
ctrlValue.year(normalizedYear.year());
此.date.setValue(ctrlValue);
}
chosenMonthHandler(标准化月份:时刻,日期选择器:MatDatepicker){
const ctrlValue=new FormControl(矩()).value;//单击时初始化矩
ctrlValue.month(normalizedMonth.month());
此.date.setValue(ctrlValue);
datepicker.close();
}
只有对文档的修改才能从初始化中删除矩()。
参考:

如果您不加载moment.js,则默认为空,但我需要moment.js将日期格式更改为UK。@PeterMoss它按预期工作。我添加了一个工作小提琴->和一个默认日期值为
null
的小提琴->请提供有关您的问题的更多信息。您好,下面是我希望发生的事情-但当我添加取消注释代码以使用moment.js时,它会在每次加载页面时将默认日期设置为今天,而我需要它默认为空/空,因此用户必须自己输入/选择日期。@PeterMoss在我看来像是一个bug行为。由于文件原因,非日期的
ng型号
值无效。这是一个bug,不是一个特性。请使用类型为
date
的对象在日期选择器@github&init处创建一个bug报告。
md datepicker不支持将
null
作为默认日期的行为->您无法实现这一点。您可能要为您的处理创建一个自定义指令。@PeterMoss顺便说一句,为什么您需要一个非预选的日期选择器?这在web应用程序中是不寻常的。如果您不加载moment.js,则默认为空,但我需要moment.js将日期格式更改为UK。@PeterMoss它按预期工作。我添加了一个工作小提琴->和一个默认日期值为
null
的小提琴->请提供有关您的问题的更多信息。您好,下面是我希望发生的事情-但当我添加取消注释代码以使用moment.js时,它会在每次加载页面时将默认日期设置为今天,而我需要它默认为空/空,因此用户必须自己输入/选择日期。@PeterMoss在我看来像是一个bug行为。由于文件原因,非日期的
ng型号
值无效。这是一个bug,不是一个特性。请使用类型为
date
的对象在日期选择器@github&init处创建一个bug报告。
md datepicker不支持将
null
作为默认日期的行为->您无法实现这一点。您可能要为您的处理创建一个自定义指令。@PeterMoss顺便说一句,为什么您需要一个非预选的日期选择器?这在web应用程序中是不寻常的。Peter有什么反馈或建议吗?Peter有什么反馈或建议吗?