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"  />
Nisham Mahsin
  • 1,399
  • 5
  • 19
  • 43

1 Answers1

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
}
SoluableNonagon
  • 11,541
  • 11
  • 53
  • 98