admin管理员组

文章数量:1289421

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.

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.

Share Improve this question edited Apr 22, 2015 at 19:41 hunch_hunch 2,3311 gold badge22 silver badges26 bronze badges asked Apr 15, 2015 at 13:44 Jhony BlazeJhony Blaze 2511 gold badge5 silver badges15 bronze badges 7
  • 1 You can do like this ` $scope.form.fieldName.$dirty = true;` – Anita Commented Apr 15, 2015 at 13:54
  • I tried and throws me an error. Cannot read property 'name' of undefined – Jhony Blaze Commented Apr 15, 2015 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 Commented Apr 15, 2015 at 14:07
  • @JhonyBlaze they way you worded your answer is a little confusing. Could you please be a little more clear. – Christian Commented Apr 15, 2015 at 14:08
  • @Anita can I initialise it in the HTML or in the controllers – Jhony Blaze Commented Apr 15, 2015 at 14:24
 |  Show 2 more ments

3 Answers 3

Reset to default 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";
}
$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'){..}

This should do the job

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

本文标签: javascriptHow to setdirty to a single inputStack Overflow