0

我是 Angular 2 的新手,我正在制作“用户注册”表单,但它在电话号码验证中的“类型上不存在属性”中显示错误。

我在 JIT 和 AOT 中编译。

在 JIT 编译器中显示错误消息并运行我的用户注册表单它工作正常。但是当我用 AOT 编译时显示编译错误。

我正在阅读 Angular 2 表单验证官方文档,这里是链接Angular 2 表单验证

下面是我的 Html 表单和编译器输出

HTML 表单

<form class="ui large form" method="post" (ngSubmit)="onSubmit(registerForm.form.valid)" #registerForm="ngForm" novalidate>
        <sm-loader [complete]="!formSubmited" class="inverted" text="Loading..."></sm-loader>
        <div class="form-group">
            <div class="col-md-12 img-bottom">
                <div class="col-md-offset-4">
                    <img (click)="profileImage.click()" class="img img-responsive img-circle ui middle aligned tiny circular image profileImage"
                        [src]="profileImageSrc" />
                    <input class="form-control user-reg-img" (change)="fileChangeEvent($event)" type="file" accept="image/*" #profileImage [hidden]="true"
                    />
                </div>
                <div class="col-md-12 image-padding">
                    <span class="user-reg-img-name" (click)="profileImage.click()">{{profileImageName}}</span>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label><strong>Signup with Email Address:</strong></label>
        </div>

        <div class="form-group">
            <p class="success text-center">{{successMessage}}</p>
            <p class="error text-center">{{errorMessage}}</p>
            <div class="field">
                <input type="text" name="name" placeholder="Name" [(ngModel)]="register.name" #name="ngModel" required class="form-control input-reg"
                />
                <div [hidden]="name.valid || name.pristine" class="error text-left">
                    Name is required
                </div>
            </div>
        </div>

        <div class="form-group">
            <div class="field">
                <input class="form-control input-reg" type="email" name="email" placeholder="Email" [(ngModel)]="register.email" #email="ngModel"
                    required />
                <div [hidden]="email.valid || email.pristine" class="error text-left">
                    Email is required
                </div>
            </div>
        </div>


        <div class="form-group">
            <input type="text" id="mobile" class="form-control  input-reg" required minlength="10" maxlength="10" name="mobile" [(ngModel)]="register.mobile"
                placeholder="Phone Number" #mobile="ngModel" pattern="[0-9]+">
            <div *ngIf="mobile.errors && (mobile.dirty || mobile.touched)">
                <div class="error text-left" [hidden]="!mobile.errors.required">
                    Mobile Number is required
                </div>
                <div class="error text-left" [hidden]="!mobile.errors.minlength">
                    Mobile Number must be at least 10 characters long.
                </div>
                <div class="error text-left" [hidden]="!mobile.errors.maxlength">
                    Monile Number cannot be more than 10 characters long.
                </div>
            </div>
        </div>

        <div class="form-group">
            <div class="field">
                <input class="form-control input-reg" type="password" name="password" placeholder="Password" [(ngModel)]="register.password"
                    #password="ngModel" required />
                <div [hidden]="password.valid || password.pristine" class="error text-left">
                    Password is required
                </div>
            </div>
        </div>

        <div class="form-group">
            <input type="checkbox" name="isAgree" [(ngModel)]="register.isAgree" #isAgree="ngModel" required />
            <label id="label-terms-cond">I Agree to Post Bucks <a href="#">Terms & Conditions</a></label>
            <div [hidden]="!showTermsAgreeErrorMsg" class="error text-left">
                Please agree the terms and conditions to continue
            </div>
        </div>

        <div class="form-group">
            <textarea rows="4" class="form-control font-small terms-cond-textarea" rows="7">You must follow any policies made available to you within the Services.</textarea>
        </div>

        <div class="form-group">
            <button class="fluid yellow large ui button btn btn-block btn-warning reg-btn-submit" type="submit">Signup</button>
        </div>
    </form>

AOT 输出

AOT 错误

提前致谢

维克拉姆

4

2 回答 2

1

看起来 JIT 编译器永远不会检查 的类型mobile.errors,但 AOT 在编译时通过查看这一行来检查它:

<div *ngIf="mobile.errors && (mobile.dirty || mobile.touched)">

根据这一行mobile.errors是一个布尔值,你会得到你的痕迹......我无法重现这个错误(版本问题?),但也许下面的代码会修复它:

<div *ngIf="(mobile.errors != null) && (mobile.dirty || mobile.touched)">

这里有一个未解决的问题:https ://github.com/angular/angular/issues/11425

于 2016-12-12T15:19:06.237 回答
-1

这是我的答案,在 mobile.errors 对象下找不到 required 的实际问题。下面是我的单行代码。

!mobile.errors['required']

如果未找到对象字段,则我们将其用作模板中的数组。

于 2016-12-13T08:21:01.697 回答