javascript 如何将 $setdirty 设置为单个输入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29651931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 10:50:28  来源:igfitidea点击:

How to $setdirty to a single input

javascripthtmlangularjsangular-ngmodelangularjs-ng-init

提问by Jhony Blaze

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 pristineclass, doesn't change, and doesn't save the new value.

我有一个小问题。我想将单个输入设置为脏,我的意思是,因为当我自动给出一个值时,它会保留在pristine类中,不会更改,也不会保存新值。

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

如果我编辑它,它会起作用并更改课程。我想取消那pristine门课。如果有人知道请告诉我。

<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 valuewith ng-modelor something, please help me.

或者,如果我做错了,我必须改变valuewithng-model或什么,请帮助我。

回答by tpie

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

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.

确保为表单和输入设置了名称 attr。

HTML Example:

HTML 示例:

<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";
}

回答by Anita

$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'){..}

当您的视图加载时,将调用 angular 调用 viewContentLoaded 事件。之后,您可以将字段设置为脏。而且如果你想调用一些方法,应该在内容加载后执行,而不是你应该在这个内部调用该方法 $scope.$on('$viewContentLoaded'){..}

回答by Hodes

This should do the job

这应该可以胜任

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

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