Angular.js programmatically setting a form field to dirty

后端 未结 11 1980
孤独总比滥情好
孤独总比滥情好 2020-12-02 10:57

I am programmatically updating some of the fields on my form with a value and I would like to set the field state to $dirty. Doing something like:

相关标签:
11条回答
  • 2020-12-02 11:19

    Made a jsFiddle just for you that solves this issue. simply set $dirty to true, but with a $timeout 0 so it runs after DOM was loaded.

    Find it here: JsFiddle

    $timeout(function () {
      $scope.form.uName.$dirty = true;
    }, 0);
    
    0 讨论(0)
  • 2020-12-02 11:20

    If you have access to the NgModelController (you can only get access to it from a directive) then you can call

    ngModel.$setViewValue("your new view value");
    // or to keep the view value the same and just change it to dirty
    ngModel.$setViewValue(ngModel.$viewValue);
    
    0 讨论(0)
  • 2020-12-02 11:24

    you will have to manually set $dirty to true and $pristine to false for the field. If you want the classes to appear on your input, then you will have to manually add ng-dirty and remove ng-pristine classes from the element. You can use $setDirty() on the form level to do all of this on the form itself, but not the form inputs, form inputs do not currently have $setDirty() as you mentioned.

    This answer may change in the future as they should add $setDirty() to inputs, seems logical.

    0 讨论(0)
  • 2020-12-02 11:28

    Small additional note to @rmag's answer. If you have empty but required fields that you want to make dirty use this:

    $scope.myForm.username.$setViewValue($scope.myForm.username.$viewValue !== undefined 
        ? $scope.myForm.username.$viewValue : '');
    
    0 讨论(0)
  • 2020-12-02 11:31

    Since AngularJS 1.3.4 you can use $setDirty() on fields (source). For example, for each field with error and marked required you can do the following:

    angular.forEach($scope.form.$error.required, function(field) {
        field.$setDirty();
    });
    
    0 讨论(0)
  • 2020-12-02 11:32

    Angular 2

    For anyone looking to do the same in Angular 2 it is very similar apart from getting a hold of the form

    <form role="form" [ngFormModel]="myFormModel" (ngSubmit)="onSubmit()" #myForm="ngForm">
    <div class="form-group">
        <label for="name">Name</label>
        <input autofocus type="text" ngControl="usename" #name="ngForm" class="form-control" id="name" placeholder="Name">
        <div [hidden]="name.valid || name.pristine" class="alert alert-danger">
            Name is required
        </div>
    </div>
    </form>
    <button type="submit" class="btn btn-primary" (click)="myForm.ngSubmit.emit()">Add</button>
    

    import { Component, } from '@angular/core';
    import { FormBuilder, Validators } from '@angular/common';
    
    @Component({
        selector: 'my-example-form',
        templateUrl: 'app/my-example-form.component.html',
        directives: []
    })
    export class MyFormComponent {
        myFormModel: any;
    
        constructor(private _formBuilder: FormBuilder) {
            this.myFormModel = this._formBuilder.group({
                'username': ['', Validators.required],
                'password': ['', Validators.required]
            });
        }
    
        onSubmit() {
            this.myFormModel.markAsDirty();
            for (let control in this.myFormModel.controls) {
                this.myFormModel.controls[control].markAsDirty();
            };
    
            if (this.myFormModel.dirty && this.myFormModel.valid) {
                // My submit logic
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题