0

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 ??

2 Answers 2

1

$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')};
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.