admin管理员组

文章数量:1289507

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

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

Share Improve this question edited Mar 8, 2016 at 15:22 MIDE11 asked Mar 8, 2016 at 15:21 MIDE11MIDE11 3,5507 gold badges39 silver badges57 bronze badges 4
  • what is a dirty element? – Rajaprabhu Aravindasamy Commented Mar 8, 2016 at 15:22
  • @RajaprabhuAravindasamy: A field that it's value was changed – MIDE11 Commented Mar 8, 2016 at 15:23
  • You can use $watch to see every time a value of an Angular variable is changed. – AleOtero93 Commented Mar 8, 2016 at 15:25
  • Is this in a form? If in a form use the formName object – charlietfl Commented Mar 8, 2016 at 15:25
Add a ment  | 

2 Answers 2

Reset to default 5

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

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

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

本文标签: javascriptAngular amp HTMLcheck if element is dirtyStack Overflow