【问题标题】:Not returning any data after promise in Ionic 3在 Ionic 3 中承诺后不返回任何数据
【发布时间】:2019-05-20 18:29:29
【问题描述】:

您好,我目前正在开发一个 Ionic 应用程序,该应用程序必须在使用 SOAP ReadData 获取数据后向用户显示一些数据到 Form Group

我正在调用我的函数,然后将数据显示到表单中,唯一的问题是表单没有显示。

constructor(
  public navCtrl: NavController,
  public navParams: NavParams,
  private privacyProvider: PrivacyProvider,
  private formBuilder: FormBuilder
  ) {

  this.myParam = navParams.get('myParam');

  console.log(this.myParam);

  this.getAnagrafica().then(() => {
    console.log(this.iobAnagrafica);
    debugger;
    this.formAnagrafica = this.formBuilder.group({
      ID_INSTALLATO: new 
 FormControl(this.iobAnagrafica.id_installato),
      ID_ANAGRAFICA: new FormControl(this.iobAnagrafica.id_anag),
      ID_PRODUTTORE: new 
 FormControl(this.iobAnagrafica.id_produttore),
      ID_GRUPPO: new FormControl(this.iobAnagrafica.id_gruppo),
      ID_INSTALLATORE: new 
 FormControl(this.iobAnagrafica.id_installatore)
      });
   });

 }

 getAnagrafica(){
  return new Promise((resolve, reject) =>{


this.privacyProvider.getIOBAnagrafica(this.myParam).subscribe((data)=> {
   if (data) {
     this.iobAnagrafica = data;
     resolve(true);
   } else {
      reject();
   }
 })
});
}

我该如何解决这个错误? 这是我的 HTML:

<ion-content>
<ion-list *ngIf="formLoaded">
<form [formGroup]="formAnagrafica">
  <ion-item>
    <ion-label stacked>ID INSTALLATO</ion-label>
    <ion-input formControlName="ID_INSTALLATO" type="text"></ion-input>
  </ion-item>
  <ion-item>
    <ion-label stacked>ID ANAGRAFICA</ion-label>
    <ion-input formControlName="ID_ANAGRAFICA" type="text"></ion-input>
  </ion-item>
  <ion-item>
    <ion-label stacked>ID PRODUTTORE</ion-label>
    <ion-input formControlName="ID_PRODUTTORE" type="text"></ion-input>
  </ion-item>
  <ion-item>
    <ion-label stacked>ID GRUPPO</ion-label>
    <ion-input formControlName="ID_GRUPPO" type="text"></ion-input>
  </ion-item>
  <ion-item>
    <ion-label stacked>ID INSTALLATORE</ion-label>
    <ion-input formControlName="ID_INSTALLATORE" type="text"></ion-input>
  </ion-item>
</form>

【问题讨论】:

  • 你的 console.log(this.iobAnagrafica);返回 ?你的承诺是否正确解决?
  • 是的,承诺正确解决。
  • 能否也提供您的模板代码?
  • 模板代码是什么意思?
  • 你的 .html 文件

标签: typescript ionic-framework ionic3


【解决方案1】:

我认为您应该在您的情况下使用 ngModelhttps://ionicframework.com/docs/developer-resources/forms/ 在你的 ts 中声明一个变量

public callBack;
constructor(
  public navCtrl: NavController,
  public navParams: NavParams,
  private privacyProvider: PrivacyProvider,
  private formBuilder: FormBuilder
  ) {

  this.myParam = navParams.get('myParam');

  console.log(this.myParam);

  this.getAnagrafica().then((chart) => {
    this.callBack = chart;
 }
...

之后,它应该显示在字段中,您也可以将 callBack 变量用于 ngIf

<ion-content>
<ion-list *ngIf="callBack">
<form [formGroup]="formAnagrafica">
  <ion-item>
    <ion-label stacked>ID INSTALLATO</ion-label>
    <ion-input [(ngModel)]="callBack.id_installato" formControlName="ID_ANAGRAFICA" type="text"></ion-input>
  </ion-item>
...

【讨论】:

    【解决方案2】:

    您似乎从未将 formLoaded 标志设置为 true。

    试试这个:

    this.getAnagrafica().then(() => {
        console.log(this.iobAnagrafica);
        debugger;
        this.formAnagrafica = this.formBuilder.group({
          ID_INSTALLATO: new 
     FormControl(this.iobAnagrafica.id_installato),
          ID_ANAGRAFICA: new FormControl(this.iobAnagrafica.id_anag),
          ID_PRODUTTORE: new 
     FormControl(this.iobAnagrafica.id_produttore),
          ID_GRUPPO: new FormControl(this.iobAnagrafica.id_gruppo),
          ID_INSTALLATORE: new 
     FormControl(this.iobAnagrafica.id_installatore)
          });
    
          this.formLoaded = true;
       });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-04
      • 2019-10-21
      • 2018-03-27
      • 2020-01-08
      • 2016-08-19
      • 1970-01-01
      • 2019-10-31
      • 1970-01-01
      相关资源
      最近更新 更多