Eduard Arevshatyan Eduard Arevshatyan - 22 days ago 9
TypeScript Question

Error with DatePicker

I have some application in angular 2 with typescript.

Now, my task is do some

<input />
tag with
type="date"
, and add into this tag
<input type="date">
some
[value]
.

My value is data from server, i get this data with help http-service (
Get
).

Something like this:
<input type="date" [value]="...">

I know that
<input type="date">
require a string, and i create some interface in my component:

export interface IDateTime {
createdat: string;
}


And of course i add this property in component:

public dateTime: IDateTime = {createdat: ""};


Finally, in template i add this tag:

<input class="form-control" type="date" [value]="IDateTime.createdat"/>


And now i've got this error:
Cannot read property 'createdat' of undefined
.
Tell me please, why cannot read property, and how to solve this problem.

Thank you.

Answer

You aren't binding to the variable you declared in your component, rather you are attempting to bind to your interface declaration's property. To bind to the variable instead you would need to have [value]="dateTime.createdat"

Comments