【问题标题】:angularjs2 formGroup and formControlNameangularjs2 formGroup 和 formControlName
【发布时间】:2018-01-06 21:55:01
【问题描述】:

我是这个 angularjs2 的新手,我刚刚创建了一个注册 使用formGroup formControlName 页面的页面并收到将空值传递给对象的错误。

HTML 代码

<div class="col-md-8 col-md-offset-2">
    <form [formGroup]="myForm" (ngSubmit)="onSubmit()">
        <div class="form-group">
            <label for="firstName">firstname</label>
            <input 
                    type="text" 
                    id="firstName" 
                    class="form-control"
                    formControlName="firstName">
        </div>
        <div class="form-group">
            <label for="lastName">lastName</label>
            <input 
                    type="text" 
                    id="lastName" 
                    class="form-control"
                    formControlName="lastName">
        </div>
        <div class="form-group">
            <label for="email">Mail</label>
            <input 
                    type="email" 
                    id="email" 
                    class="form-control"
                    formControlName="email">
        </div>
        <div class="form-group">
            <label for="password">password</label>
            <input 
                    type="password" 
                    id="password" 
                    class="form-control"
                    formControlName="password">
        </div>
        <button 
            class="btn btn-primary" 
            type="submit"
            [disabled]="!myForm.valid">Submit</button>
    </form>
</div>


<!--
    [formGroup]="myForm" is used to instruct the angular2 to don't use ur own form use mine.'

    formControlName is used to identify which attribute to match in SignupComponent class 
    -->

signup.component.ts 文件

 import { Component, OnInit} from '@angular/core';
    import { FormGroup, FormControl, Validators} from '@angular/forms';

    import { AuthService} from './auth.service';
    import { User} from './user.model';

    @Component({
        selector:'app-signup',
        templateUrl: './signup.component.html'
    })
    export class SignupComponent implements OnInit{
        myForm: FormGroup;

        constructor(private authService : AuthService) {}

        onSubmit(){

                  console.log(this.myForm);

            this.myForm.reset();
        }

        ngOnInit(){
            this.myForm = new FormGroup({
                firstName: new FormControl(null, Validators.required),
                lastName: new FormControl(null, Validators.required),
                email: new FormControl(null, [
                    Validators.required,
                    Validators.pattern("[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}")
                ]),
                password: new FormControl(null, Validators.required),

            });
        }
    }

将值传递给对象时出现此错误,它给出了 将空值传递给对象时出错

enter image description here

【问题讨论】:

    标签: html node.js typescript angular2-forms


    【解决方案1】:

    请将表单对象的引用解析成onSubmit()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-12
      • 2017-11-13
      • 2017-08-12
      • 2019-07-28
      • 1970-01-01
      • 1970-01-01
      • 2018-01-31
      • 2017-04-01
      相关资源
      最近更新 更多