ERROR使用setValue angular 4时缺少位置0处的数字 [英] ERROR Missing number at position 0 when using setValue angular 4
问题描述
我正在使用带有反应形式的 Angular 4 , momentjs ,以及 primeng calendar I'我想绑定使用 setValue
并在包含Date的reactiveForm字段上尝试 patchValue
。此日期已通过primeng日历创建。
I'm using Angular 4 with reactiveforms, momentjs, and primeng calendar I'm tying to use setValue
and have tried patchValue
on a reactiveForm field which contains a Date. This date has been created via a primeng calendar.
purchaseDate:2017年9月2日星期六00:00:00 GMT + 0100(GMT日光时间)
我使用这个'日期'做了几件事,然后使用 momentjs
使用 .moment()。格式(.... $ c $)为后端接受(即YYYY.MM.DD)的干净格式c>
I use this 'date' to do a couple of things and then onSumbit of the form I convert the date using momentjs
to a clean format ready for the backend to accept (i.e. YYYY.MM.DD) using.moment().format(....
然而,当我运行 .setValue
时,我收到以下控制台错误错误位置0缺少数字
并且无法找出原因。
However when I run the .setValue
I'm getting the following console error ERROR Missing number at position 0
and can't figure out why.
// convert the date
let newDate = moment(this.form.get('purchaseDate').value).format('YYYY.MM.DD');
// let newDate = moment(this.form.get('purchaseDate')).format('YYYY.MM.DD');
// with or without .value - display the same below (2017.09.01)
console.log(newDate); // 2017.09.01
this.form.get('purchaseDate').setValue(newDate);
// if I create a seperate (empty) reactiveForms field to test against
this.form.get('testField').setValue(newDate) // this works fine
我已经将问题追溯到我尝试设置/修补primeng日历值时 - 由于某种原因不喜欢被更改。
I have traced the issue down to when i try to set / patch the primeng calendar value - for some reason is doesn't like to be changed.
更新 monent格式
问题现在发生在setValue上,现在出现以下错误
viewWrappedDebugError位置2的意外文字
The issue seams to be happening on the setValue now getting the following error
Unexpected literal at position 2 at viewWrappedDebugError
推荐答案
是PrimeNG日期选择器期望接收 Date
类的实例作为值,并返回 Date
类的实例值。所以这就是为什么你试图把其他类型的对象放在那里失败的原因。
The thing is that PrimeNG date picker expects to receive instance of Date
class as a value and it returns instance of a Date
class as value. So that's why your attempts to put object of other types in there failed.
目前还不清楚为什么你试图调用 setValue()
在提交处理程序中。
It's not clear why you attempt to call setValue()
in the submit handler.
如果您的目标是以编程方式修改值,请遵循VincenzoC在评论中的建议 - 修改对象并将其转换回日期
对象,然后将其传递给 setValue()
。
If your goal is to modify value programmatically follow suggestion from VincenzoC in comments - modify object and transform it back to Date
object before passing it to setValue()
.
let newDate: Date = moment(this.form.get('purchaseDate').value).add(5, 'days').toDate();
this.form.get('purchaseDate').setValue(newDate);
如果您的目标是格式化日期
对象要提交到后端,您根本不需要调用 setValue()
。格式日期
将对象转换为字符串,并将此字符串而不是 Date
对象传递给后端API。如果您从表单提交整个值
对象,则可以在提交之前以这种方式进行修改:
If your goal is to format Date
object for submitting to backend, you don't need to call setValue()
at all. Format Date
object to string and pass this string instead of Date
object to the backend API. If you submitting whole value
object from form you can modify it this way before submitting:
let newDate: string = moment(this.form.get('purchaseDate').value).format('YYYY.MM.DD');
let dataForBackend = { ...this.form.value, purchaseDate: newDate };
this.myBackend.sendData(dataForBackend);
这篇关于ERROR使用setValue angular 4时缺少位置0处的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!