0

I want to assign my string date to ui-date

my view

 <input ng-switch-when="datePicker" type="text" ui-date-format ui-date class="form-control" ng-model="field.value" />

and my date string "12/12/2015"

but I got this error "Error: ng-Model value must be a Date object - currently it is a string - use ui-date-format to convert it from a string"

Please someone help me, thank

Sophon Men
  • 179
  • 1
  • 2
  • 10

1 Answers1

1

Try doing as it says (Error: ng-Model value must be a Date object) in controller

$scope.field.value = new Date($scope.field.value);

Or add an date-format attribute with your date mask to the input tag

ui-date-format="DD/MM/YYYY"
shershen
  • 9,875
  • 11
  • 39
  • 60
  • I have tried your first option,It's not error, but the view (input) is blank the second option , I still got the same error. – Sophon Men Feb 01 '16 at 09:32
  • oh, sorry thats should be - ui-date-format="mm/dd/yy" – shershen Feb 01 '16 at 09:41
  • could you show all the code html/js of your case? - seems that something else results this error – shershen Feb 01 '16 at 10:04
  • I can not reply your comment with code style, so i have posted other answer for you – Sophon Men Feb 01 '16 at 10:23
  • ohhhh, its a mess >_< can you in your template add near to this line ' ' the following - '{{field.value}}' - so that we can see what value is there – shershen Feb 01 '16 at 10:31
  • IT shows 12/12/2015 , while JSON "12/12/2015" and In textbox "Sat Dec 12 2015 00:00:00 GMT+0700 (ICT)" and got a error like I mention in question – Sophon Men Feb 01 '16 at 10:35
  • let's remove type="text" attribute from that input – shershen Feb 01 '16 at 10:42
  • still the same, I think the problem is about ng-model. – Sophon Men Feb 01 '16 at 10:45
  • did you remove that line '$scope.field.value = new Date($scope.field.value);' I wrote initially? that should not be there – shershen Feb 01 '16 at 10:47
  • could you maybe reproduce the full code of your case in http://codepen.io or http://plnkr.co/ ? we're definitely missing something obvious, but it's hard to debug code in your mind – shershen Feb 01 '16 at 12:28