4

I have the following code in my HTML page:

<input type="text" name="dns" class="form-control ng-valid" ng-model="conf['dns']" ng-list="">

When I type something in the text field, its changed to:

<input type="text" name="dns" class="form-control ng-valid ng-dirty" ng-model="conf['dns']" ng-list="">

I wish to check if the field its dirty, and do some acts if so. I have the following:

Controller.controller('CuController',
    function($scope, $location, $modal, Controller) {
    console.log($scope.conf['dns']) //prints the value of this field
    // Wish to check if $scope.conf['dns'] is dirty
})

I try to use $scope.conf[dns].$dirty, but it returns undefined.

How can I check if the field is dirty (Meaning that the value of the field was changed)?

4
  • what is a dirty element? Commented Mar 8, 2016 at 15:22
  • @RajaprabhuAravindasamy: A field that it's value was changed Commented Mar 8, 2016 at 15:23
  • You can use $watch to see every time a value of an Angular variable is changed. Commented Mar 8, 2016 at 15:25
  • Is this in a form? If in a form use the formName object Commented Mar 8, 2016 at 15:25

2 Answers 2

5

$dirty is a parameter of the input of your form, try $scope.yourForm.dns.$dirty

Sign up to request clarification or add additional context in comments.

1 Comment

It seems that it doesnt know the form structure: TypeError: Cannot read property 'dns' of undefined
4

You can access form properties through the form name like so:

$scope.myForm.dns.$dirty; // boolean

1 Comment

It seems that it doesnt know the form structure: TypeError: Cannot read property 'dns' of undefined

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.