Ionic 3表单未将值发送到后端

时间:2019-02-06 09:48:46

标签: ionic-framework ionic3

我们正在尝试登录该应用程序。关键是,使用相同的基于Angular的后端网站可以正常运行。仅离子性会发生此问题。填写登录表单输入时,不允许抛出空白字段。此错误消息来自后端。这意味着它不会将值发送到后端。这是代码:

export class LoginPage {
  loading: any;
  tabElement: any;
  SigninForm: FormGroup;
  forbiddenEmails: any;
  constructor(
    public navCtrl: NavController,
    public navParams: NavParams,
    private authProviders: AuthProvider,
    private alertCtrl: AlertController,
    private loadingCtrl: LoadingController,
    private tokenProvider: TokenProvider,
    private formBuilder: FormBuilder,
  ) {
    this.tabElement = document.querySelector('.tabbar.show-tabbar');
    this.SigninForm = this.formBuilder.group({
      username: [null, [Validators.required]],
      password: [null, [Validators.required, Validators.minLength(4)]],
    });
  }

  ionViewDidLoad() {
    if (this.tabElement) {
      (this.tabElement as HTMLElement).style.display = 'none';
    }
  }

  ionViewWillEnter() {
    if (this.tabElement) {
      (this.tabElement as HTMLElement).style.display = 'none';
    }
  }




  LoginUser() {
    this.ShowLoader();
    this.authProviders.LoginUser(this.SigninForm.value).subscribe(
      data => {
        this.tokenProvider.SetToken(data.token);
        setTimeout(() => {
          this.loading.dismiss();
          this.navCtrl.setRoot('TabsPage');
        }, 2000);
      },
      err => {
        this.loading.dismiss();
        if (err.error.msg) {
          this.SHowErrorAlert(err.error.msg[0].message);
        }

        if (err.error.message) {
          this.SHowErrorAlert(err.error.message);
        }
      }
    );
  }

  RegisterPage() {
    this.navCtrl.push('RegisterPage');
  }

  SHowErrorAlert(message) {
    let alert = this.alertCtrl.create({
      title: 'Login Error',
      subTitle: `${message}`,
      buttons: ['OK'],
      cssClass: 'alertCss'
    });

    alert.present();
  }

  ShowLoader() {
    this.loading = this.loadingCtrl.create({
      content: 'Athenticating...'
    });
    this.loading.present();
  }
}

HTML

<ion-content class="sign" padding>
  <div class="layer">
  <div class="appForm" ion-fixed padding margin-bottom style="position: absolute !important; top: 0%; bottom: 0%; left: 0; margin-bottom: 1%;">
      <form [formGroup]="SigninForm">
    <ion-list style="margin-top: 30% !important">
      <ion-item>
          <div class="logo">
              <h2>MyDreamLove</h2>
            </div>
      </ion-item>
      <ion-item>
        <ion-input class="input-field" formControlName="username" name="username" type="text" placeholder="Username"></ion-input>
      </ion-item>
      <div *ngIf="!SigninForm.get('username').valid && SigninForm.get('username').touched"
      class="help-block">Please enter a valid username!</div>

      <ion-item>
        <ion-input class="input-field" formControlname="password" name="password" type="password" placeholder="Password"></ion-input>
      </ion-item>
      <div *ngIf="!SigninForm.get('password').valid && SigninForm.get('password').touched"
      class="help-block">Please enter a valid password!</div>
      <button ion-button block class="login-button" (click)="LoginUser()">Login</button>

      <p class="signin">
        <button class="register-button" ion-button block clear (click)="RegisterPage()">Sign Up</button>
      </p>
    </ion-list>
</form>
  </div>
  </div>
</ion-content>

我的队友在此处发布了完整代码: https://github.com/ionic-team/ionic/issues/17398

为什么它不将输入值发送到后端进行检查?

1 个答案:

答案 0 :(得分:0)

在您的代码中,当您按下“登录”按钮时,密码将始终为空。

原因:

在HTML的“密码”字段中,您指定的是 formControlname ,而不是 formControlName 。请注意,名称中的大写字母 N 是大写字母。尝试更正它,看看是否可行。