MIDE11
MIDE11

Reputation: 3550

Angular & HTML / check if element is dirty

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

Upvotes: 4

Views: 8358

Answers (2)

inorganik
inorganik

Reputation: 25535

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

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

Upvotes: 4

Charles
Charles

Reputation: 180

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

Upvotes: 5

Related Questions