irfan shafi
irfan shafi

Reputation: 524

Setting the default date for html 5 date type with angularjs

i have to set the today,s date as default in html date type , i am able to do that as this check this working fiddle click here , But its not working when i am changing the ng-model to my.date

    <input type="date" ng-model="my.date" value={{my.date}}>
    //angular controller
     function MyCtrl($scope, $filter) {
     $scope.my.date = $filter("date")(Date.now(), 'yyyy-MM-dd');
}     

why so ??

Upvotes: 0

Views: 400

Answers (2)

Coder
Coder

Reputation: 7076

When you put variables inside object, you need to declare that object first.

In your controller declare the $scope.my object first.

HTML

<input type="date" ng-model="my.date" > 

Controller JS

$scope.my = {};
    $scope.my.date = $filter("date")(Date.now(), 'yyyy-MM-dd');

You don't need to set the value in input. Once the value of ng-model is set, it is bind to input field.

Updated JSFiddle

Upvotes: 1

New Dev
New Dev

Reputation: 49590

$scope.my is undefined and you are trying to assign the property date to it - that's why it doesn't work. Instead, define it like so:

$scope.my = {};
$scope.my.date = $filter("date")(Date.now(), 'yyyy-MM-dd');

Or, equivalently.

$scope.my = {date: $filter("date")(Date.now(), 'yyyy-MM-dd')};

Upvotes: 1

Related Questions