Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use string interpolation in html "value" attribute

Tags:

angular

I use the below to generate date in component.ts file:

this.myDate=new Date();

And try to use in html form value field.But nothing is loaded.I have tried both the below syntax but not succesful

<input type="datetime" class="form-control" id="date" value={{this.myDate}} name="created" ngModel>

<input type="datetime" class="form-control" id="date" [value]=[this.myDate] name="created" ngModel>

One more issue is that my json file is upadted with only empty string after add operation

like image 786
Mohammed Ajmal Avatar asked Jul 03 '26 09:07

Mohammed Ajmal


1 Answers

You are missing the right date format for the input type datetime that is ="2017-06-01T08:30"

in the component.ts

formatted_date:string;

ngOnInit(){
//your code...
this.formatted_date = this.myDate.toISOString();
this.formatted_date.slice(0,16);
}

In the template.html (change the type to datetime-local beacuse datetime is Obsolete MDN Doc)

<input type="datetime-local" class="form-control" id="date" value="{{formatted_date}}" name="created">
like image 142
Vash72 Avatar answered Jul 05 '26 07:07

Vash72



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!