0

I have a date of birth field and a age text box.How can i update age text box automatically when selecting date of birth in angularJs. code for inputs

 <label for="" class="col-md-2 control-label">Dob</label>
  <input type="datepicker"class="form-control" my-date-picker required="required" ng-model="newItem.dob" />
  <label for="" class="col-md-2 control-label">Age</label>
  <input type="text" class="form-control"  disabled="true"  ng-model="newItem.age"  />
5
  • 3
    Can you please post the coe you've already tried? Commented Feb 5, 2014 at 16:51
  • possible duplicate of How can I calculate the number of years betwen two dates? Commented Feb 5, 2014 at 16:53
  • @MikeRobinson .i need solution in angularjs Commented Feb 5, 2014 at 16:58
  • @NishamMahsin you can use $watch. Just watch for the changes made to dob and update age in the callback. Commented Feb 5, 2014 at 17:00
  • You should at least attempt a solution, demonstrate you've read the help docs and post some sample code. Commented Feb 5, 2014 at 18:18

1 Answer 1

2

Something like this, add ng-change to your first input, call a function when a input change is made. The function must be declared in the scope.

<label for="" class="col-md-2 control-label">Dob</label>
<input type="datepicker" ng-change='myAgeFunction()' class="form-control" my-date-picker required="required" ng-model="newItem.dob" />
<label for="" class="col-md-2 control-label">Age</label>
<input type="text" class="form-control"  disabled="true"  ng-model="newItem.age"  />

Javascript in your controller:

$scope.newItem = {dob: "00/00/0000", age: 0 } // your object which refers to ng-model
$scope.myAgeFunction = function(){ //your function goes here
    $scope.newItem.age = ... code to calculate age; // update the model
}
Sign up to request clarification or add additional context in comments.

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.