【问题标题】:Dynamic form from JSON in ionic 2ionic 2 中 JSON 的动态表单
【发布时间】:2018-10-17 22:57:03
【问题描述】:

我的 JSON 数组是这样的

Array(3)
main2=
[

    0:{label: "Name", datatype: "text", lookupname: "null", order: 01"}

    1: {label: "DOB", datatype: "date", lookupname: "null", order: "02"}

   2: {label: "QRcode", datatype: "qrcode", lookupname: "null", order: "02"}

   3: {label: "Image", datatype: "image", lookupname: "null", order: "02"}

  ]

我尝试根据数据类型生成输入表单。但我不能在循环内包含按钮,循环内没有 HTML 标签工作,只有像 ion-something 工作的标签。喜欢在数据类型为条形码或时添加按钮循环内的二维码

我在 ionic 上的 HTML

    <form>
          <ion-item *ngFor="let item of main2">
          <ion-label fixed>{{item.label}} : </ion-label>
          <ion-input type="text"  name="title" *ngIf='item.datatype == "text"'></ion-input>
          <ion-input type="text" *ngIf='item.datatype == "radio"'>Checkbox 1</ion-input>
          <ion-input type="number" *ngIf='item.datatype == "number"'></ion-input>
          <ion-datetime displayFormat="MMMM/DD/YYYY" *ngIf='item.datatype == "date"'></ion-datetime>
          <ion-input type="file"  name="title" *ngIf='item.datatype == "image"'></ion-input>
          <ion-datetime displayFormat="HH:mm" *ngIf='item.datatype == "time"'></ion-datetime>
          <ion-input type="number" *ngIf='item.datatype == "number"'></ion-input>

          <ion-input type="text"  *ngIf='item.datatype == "qrcode"' ></ion-input>
          <ion-input type="text"   *ngIf='item.datatype == "barcode"'></ion-input>

</form>

【问题讨论】:

    标签: javascript angular ionic-framework ionic2


    【解决方案1】:
    1. 您的 JSON 数组缺少一个 " for order 字段:

      0:{label: "Name", datatype: "text", lookupname: "null", order: 01"}

    2. 您的 html 缺少 &lt;ion-item&gt; 的结束标记

    3. 添加按钮:

    (选项 A)ion-item 中的按钮:您必须使用特定语法 item-leftitem-right (https://ionicframework.com/docs/2.3.0/api/components/item/Item/)

    (选项B)离子项作为按钮:您可以选择将离子项作为按钮

    HTML

    <ion-content>
      <form *ngFor="let item of main2">
        <ion-item>
          <ion-label fixed>{{item.label}} : </ion-label>
          ...
          //Option A
          <button ion-button item-right *ngIf='item.datatype == "qrcode"'>Btn</button>
        </ion-item>
    
        //Option B
        <button ion-item *ngIf='item.datatype == "qrcode"' (click)="test()">{{item.label}} Button</button>
      </form>
    </ion-content>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-14
      相关资源
      最近更新 更多