如何$ setdirty到单个输入 [英] How to $setdirty to a single input

查看:107
本文介绍了如何$ setdirty到单个输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

如果我编辑它,它可以工作并更改课程。我想取消 pristine 类。
如果有人知道,请告诉我。

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>

脚本:

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

或者,如果我做错了,我必须更改 ng-model 或其他什么,请帮助我。

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.

确保为表格设置名称attr输入。

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

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>

一个简单的函数:

$scope.addText = function() {
  $scope.myText = "now I'm dirty";
}

这篇关于如何$ setdirty到单个输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆