如何将 $setdirty 设置为单个输入
[英] How to $setdirty to a single input
本文介绍了如何将 $setdirty 设置为单个输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个小问题.我想将单个输入设置为脏,我的意思是,因为当我自动给出一个值时,它会保留在 pristine
类中,不会改变,也不会保存新值.
如果我编辑它,它会起作用并更改课程.我想取消那个 pristine
类.如果有人知道请告诉我.
<div class="form-group"><label for="name" class="col-md-2 control-label">学生姓名</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">
</表单>
和脚本:
document.getElementbyId('name').value = "anything";
或者,如果我做错了,我必须用 ng-model
或其他东西更改 value
,请帮助我.
解决方案
http://plnkr.co/edit/bVoljJqiK3CLB2xqZ6Zm?p=preview
您可以在那里看到一个工作示例.
确保为表单和输入设置了名称 attr.
HTML 示例:
<button id="dirty-button" ng-click="addText(); myForm.myText.$setDirty()">弄脏</button><小时><表单名称="myForm"><input name="myText" id="myTextInput" type="text" ng-model="myText" required></表单><br/><span ng-show="myForm.myText.$dirty">现在很脏</span>
一个简单的函数:
$scope.addText = function() {$scope.myText = "现在我脏了";}
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.
解决方案
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";
}
这篇关于如何将 $setdirty 设置为单个输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!