6

I've got a little problem. I want to set to dirty a single input, I mean, because when I give a value automatically it stays in pristine class, doesn't change, and doesn't save the new value.

If I edit it, it works and change the class. I want to cancel that pristine class. If anyone know please let me know.

<form class="form-horizontal" ng-repeat="studiant in studiants" name="form" id="form">

  <input type="hidden" name="id" value="{{studiant.studiant_id}}" class="form-control" disabled>

  <div class="form-group">
    <label for="school" class="col-md-2 control-label">School</label>
    <div class="col-md-1">
      <input type="text" id="school" name="school" class="form-control" ng-init="studiant.school=studiant.studiant_school" ng-model="studiant.school">
    </div>
  </div>
  <div class="form-group">
    <label for="name" class="col-md-2 control-label">Student's Name</label>
    <div class="col-md-10">
      <input type="text" id="name" name="name" class="form-control" ng-init="studiant.name=studiant.studiant_name" ng-model="studiant.name">
    </div>
  </div>
</form>

And the script:

document.getElementbyId('name').value = "anything";

Or, if I doing wrong and I have to change the value with ng-model or something, please help me.

hunch_hunch
  • 2,283
  • 1
  • 21
  • 26
Jhony Blaze
  • 251
  • 1
  • 5
  • 15
  • 1
    You can do like this ` $scope.form.fieldName.$dirty = true;` – Anita Apr 15 '15 at 13:54
  • I tried and throws me an error. Cannot read property 'name' of undefined – Jhony Blaze Apr 15 '15 at 14:02
  • It might happen that form has not been intialised then wrap this inside this function ` $scope.$on('$viewContentLoaded', function() {...}` Also try after changing the name of form and name field to some other name. – Anita Apr 15 '15 at 14:07
  • @JhonyBlaze they way you worded your answer is a little confusing. Could you please be a little more clear. – Jon Doe Apr 15 '15 at 14:08
  • @Anita can I initialise it in the HTML or in the controllers – Jhony Blaze Apr 15 '15 at 14:24
  • In controller you should add this method `$scope.$on('$viewContentLoaded){ $scope.form.fieldName.$dirty = true; }` This method is called when you view content is loaded and that time the form and form field will be accessible. – Anita Apr 15 '15 at 14:27
  • @JhonyBlaze Ya sure. I am answering it ,if you like then vote up or except it – Anita Apr 16 '15 at 04:16

3 Answers3

6

http://plnkr.co/edit/bVoljJqiK3CLB2xqZ6Zm?p=preview

You can see a working example there.

Make sure you have the name attr set for the form and the input.

HTML Example:

<button id="dirty-button" ng-click="addText(); myForm.myText.$setDirty()">Make Dirty</button>
<hr> 
<form name="myForm">   
<input name="myText" id="myTextInput" type="text" ng-model="myText" required>
</form>
<br/>
<span ng-show="myForm.myText.$dirty">it's dirty now</span>

A simple function:

$scope.addText = function() {
  $scope.myText = "now I'm dirty";
}
tpie
  • 6,021
  • 3
  • 22
  • 41
  • 1
    It would be better if added the relevant parts of the code to the answer. Imagine the plnkr link is not working - how much will your answer make sense then to someone looking for answers – New Dev Apr 15 '15 at 19:58
0
$scope.$on('$viewContentLoaded'){
 $scope.form.fieldName.$dirty = true;
 }

When your view is loaded then angular calls viewContentLoaded event is called. After that you can set the field dirty. And also if you want to call some method ,that should be executed after the content is loaded than you should call that method inside this $scope.$on('$viewContentLoaded'){..}

Anita
  • 2,352
  • 2
  • 19
  • 30
-3

This should do the job

angular.element(document.querySelector('form')).scope().formname.fieldname.$setDirty()

Hodes
  • 895
  • 2
  • 10
  • 18