ryanh ryanh - 4 months ago 8
AngularJS Question

Angularjs input[time] Not Accepting Value From Controller

I am having a problem getting my data formatted properly so that angularjs will populate the data an input field with type "time". Can you help me figure out what I am missing? Thank you in advance.

HTML:

<input type="time" id="exampleInput" name="input" ng-model="stuff.starttime"
placeholder="HH:mm:ss" min="00:00:00" max="17:00:00" >


Controller:

$scope.stuff = {
originlevel2: "",
destinationlevel2: "",
destination2: "",
origin2: "",
starttime: "",
endtime: ""
}
var starttime = jsonArray2[0]["starttime"];
$scope.stuff.starttime = new Date(starttime);

Answer

Since the value of jsonArray2[0]["starttime"] is 01:00:00.

Thus, new Date(jsonArray2[0]["starttime"]) is invalid date.

You can instead the following :

var d=new Date();
starttime='01:00:00' // jsonArray2[0]["starttime"]
starttime=starttime.split(':').map((e)=>parseInt(e))
d.setHours(starttime[0]);
d.setMinutes(starttime[1]);
d.setSeconds(starttime[2]);

// Then
$scope.stuff.starttime= d;

After, that try with Chrome or safari .


Note:

Note: type="time" is not supported in Firefox, or Internet Explorer 10 and earlier versions.