2016-03-08 51 views
2

我在我的HTML页面下面的代码:角和HTML /检查元素脏

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

当我输入文本字段的东西,它改为:

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

我希望检查该领域是否肮脏,并且如果是这样的话,可以采取一些行动。我有以下几点:

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

我尝试使用$scope.conf[dns].$dirty,但它返回undefined

如何检查字段是否脏(意思是字段的值已更改)?

+0

什么是脏的元素? –

+0

@RajaprabhuAravindasamy:值已更改的字段 – MiddleWare

+0

每次更改角度变量的值时,都可以使用$ watch来查看。 – AleOtero93

回答

5

$脏表单的输入参数,尽量$scope.yourForm.dns.$dirty

+0

它似乎不知道窗体结构:TypeError:无法读取属性'dns'的undefined – MiddleWare

+0

看这里它是如何工作的:http://plnkr.co/edit/ZlbfR2zjNXkwLY3xPhQ9 – Charles

4

您可以通过表单名称访问表单属性,像这样:

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

看来它不知道窗体结构:'TypeError:不能读取未定义的属性'dns' – MiddleWare