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.
- 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
3 Answers
Reset to default 6http://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
版权声明:本文标题:javascript - How to $setdirty to a single input - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741424703a2378008.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论