Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

accessing $dirty value inside the controller

Tags:

angularjs

i have routeProvider, ng-view and controller. Simple template with form and input. I can see $dirty value in {{form.var1.$dirty}} - it changes when i type, but how to access it in the controller code?

html main

<!doctype html>
<html ng-app="project">
<head>
    <meta charset="utf-8">
    <title>Tabs</title>
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
    <link href="css/bootstrap.min.css" rel="stylesheet" media="screen">
    <script src="js/jquery.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script src="lib/angular/angular.js"></script>
    <script src="js/dirty.js"></script>
    <!--<script src="js/tab.js"></script>-->
</head><body>

            <div ng-view>

            </div>

</body>
</html>

template

{{2+2}}<br>
|{{var1}}|<br>
|{{form.var1.$dirty}}|
<a href="" ng-click="dodo1();">check dirty</a>
<form class="form-horizontal" novalidate name="form" ng-submit="submit()">
    <input id="var1" name="var1"  class="input" type="text" ng-model="var1">
</form>

js

angular.module('project',[]).
    config(function($routeProvider) {
        $routeProvider.
            when('/', {controller:Ctrl1, templateUrl:'dirty_tab.html'}).
            when('/tab1', {controller:Ctrl1, templateUrl:'dirty_tab.html'}).
            otherwise({redirectTo:'/'});
    });

function Ctrl1($scope,$rootScope) {
    $scope.var1=100;
 $scope.dodo1 = function() {
    alert(form.var1.$dirty);
 }
}

Alert shows me "undefined". How to get var1 $dirty value?

like image 950
Sol Avatar asked Dec 15 '22 05:12

Sol


1 Answers

As Cherniv told you in the comments you need to access the variable from the $scope object

$scope.form.var1.$dirty
like image 187
fabrizioM Avatar answered Dec 28 '22 22:12

fabrizioM