2

当我 ng s --aot 时收到此消息 Object is possibly 'null'.

我整个早上都在尝试解决它,有什么想法吗?错误发生在 <mdb-success *ngIf="email.valid && (email.dirty || email.touched)" class="mt-2">Email valid</mdb-success>email.valid、dirty 等对象上。

 <form class="form-inline md-form form-sm" [formGroup]="validatingForm" (submit)="update(emailInput.value)">
          <input mdbInput mdbValidate type="email" class="form-control form-control-sm mr-3 w-75" placeholder="Enter user's email" aria-label="Search" formControlName="email" #emailInput required />
          <button mdbBtn type="button" color="primary" mdbWavesEffect (click)="update(emailInput.value)">
            <mdb-icon fas icon="search" aria-hidden="true"></mdb-icon>
          </button>
          <mdb-error *ngIf="email.invalid && (email.dirty || email.touched)" class="mt-2">Email invalid</mdb-error>
          <mdb-success *ngIf="email.valid && (email.dirty || email.touched)" class="mt-2">Email valid</mdb-success>
        </form>

ts代码:

 export class UsersComponent implements OnInit {
      validatingForm: FormGroup;
      token: any;
      userDetails: {};
      hasUsers = false;
      emailValue: string;
      constructor(private crudService: CrudService) {}

      ngOnInit() {
        this.validatingForm = new FormGroup({
          email: new FormControl("", Validators.email)
        });
        this.loginAsAdmin();
      }

      get email() {
        return this.validatingForm.get("email");
      }
4

2 回答 2

1

*ngIf="email && email.valid && (email.dirty || email.touched)" @ritaj

于 2020-02-19T10:36:50.757 回答
0

只需 *ngIf="email" 即可解决该错误

于 2021-03-15T18:03:31.300 回答