How to get the $pristine state of a form when a dropdown changes value in AngularJS?

I have a dropdown, a select element, with an ng-change event handler. I would like to know if the form was pristine before the user makes a selection. If I check the $pristine value in the handler, it's already not pristine. Too late in the lifecycle. How to get the pristine state of the form and save its value just before the value of the select, or the model property behind it, changes?

1 answer

  • answered 2018-07-11 02:41 lastr2d2

    How about add another handler for ngFocus on the form, get the value and assign to a $scope variable, and read that value in your ngChange handler.

    Something like this:

    $scope.pristine = true;
    $scope.focusHandler = function() {
      $scope.pristine = $scope.myForm.$pristine;
    $scope.changeHandler = function() {
      // $scope.pristine is what your want