从JSON中未定义获取选项和选择

时间:2018-07-20 08:58:14

标签: javascript angular ionic3 angular5

{  
   "status":"true",
   "select":[  
      {  
         "view_":"select",
         "Type":"MARKET_WORKING",
         "Question":"WHICH BRAND COUNTER IS IT?",
         "options":[  
            {  
               "option":"USPA"
            },
            {  
               "option":"HANES"
            },
            {  
               "option":"USPA & HANES "
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            }
         ]
      }
   ],
   "text":[  
      {  
         "view_":"select",
         "Type":"MARKET_WORKING",
         "Question":"WHAT IS THE OPENING STOCK OF INNERWEARS TOP?",
         "options":[  
            {  
               "option":"VEST"
            },
            {  
               "option":"BRA_PADDED"
            },
            {  
               "option":"BRA_NON PADDED"
            },
            {  
               "option":"CAMISOLE"
            },
            {  
               "option":"THERMAL"
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            }
         ]
      },
      {  
         "view_":"select",
         "Type":"MARKET_WORKING",
         "Question":"WHAT IS THE OPENING STOCK OF INNERWEARS BOTTOM?",
         "options":[  
            {  
               "option":"BRIEF"
            },
            {  
               "option":"TRUNKS"
            },
            {  
               "option":"PANTIES"
            },
            {  
               "option":"THERMAL LEGGINGS"
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            }
         ]
      },
      {  
         "view_":"select",
         "Type":"MARKET_WORKING",
         "Question":"WHAT IS THE OPENING STOCK OF COMFORTWEARS TOP?",
         "options":[  
            {  
               "option":"MEN'S T SHIRT"
            },
            {  
               "option":"MEN'S TANKS"
            },
            {  
               "option":"WOMEN'S T SHIRT"
            },
            {  
               "option":"WALL FIXTURE"
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            }
         ]
      },
      {  
         "view_":"select",
         "Type":"MARKET_WORKING",
         "Question":"WHAT IS THE OPENING STOCK OF COMFORTWEARS BOTTOM?",
         "options":[  
            {  
               "option":"BOXER SHORTS"
            },
            {  
               "option":"MEN'S SHORTS"
            },
            {  
               "option":"MEN'S PANTS"
            },
            {  
               "option":"MEN'S SOCKS"
            },
            {  
               "option":"WOMEN'S SHORTS"
            },
            {  
               "option":"WOMEN'S PANTS"
            },
            {  
               "option":"WOMEN'S CAPRI"
            },
            {  
               "option":"WOMEN'S SOCKS"
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            }
         ]
      }
   ]
}

我在API中具有上述 JSON 。为此,我想在ngFor中使用Question和选项来使用它。

  if(this.user_type == 1){

        this.mwork = this.navParams.get('mwork');
        this.selectq = this.mwork.select;
        this.selecto = this.selectq.options;            
        this.textq = this.mwork.text;
        console.log(this.selecto);
    }

在控制台中,如果我编写了mwork。我正在获取数据,但是当我编写selecto时。 M获取未定义的选项。有什么问题吗?。请帮忙。

编辑: 以下是Ankit的答案,

  if(this.user_type == 1){

        this.mwork = this.navParams.get('mwork');
        this.selectq = this.mwork.select;
        this.selecto = this.selectq[0].options;
        //this.selecto = this.selectq.options;
        this.textq = this.mwork.text;
        this.texto = this.textq[0].options;

        console.log(this.selecto);
    }

但是,对于所有显示相同选项的问题。如何按问题显示

下面是我的HTML

  <div class="field-group" *ngFor="let mworkq of selectq">
        <div class="label-cust">{{mworkq.Question}}</div>
        <div class="field-cust">
          <select [(ngModel)]="selectedOption">
            <option value=''>Select</option>
            <option  *ngFor="let mworkq of selecto" value="{{mworkq.option}}">{{mworkq.option}}</option>

           <!--  <option value="{{mworkq.option4}}">{{mworkq.option4}}</option> -->

          </select>
        </div>
      </div>
      <div class="field-group" *ngFor="let mworkq of textq">
        <div class="label-cust">
              {{mworkq.Question}}
              <ion-input *ngFor="let mworkq of texto" class="{{mworkq.option}}" type="text" placeholder="{{mworkq.option}}" ></ion-input>

        </div>

1 个答案:

答案 0 :(得分:4)

this.selectq的数据类型是数组,因为this.mwork.select是具有值的数组

"select":[  
      {  
         "view_":"select",
         "Type":"MARKET_WORKING",
         "Question":"WHICH BRAND COUNTER IS IT?",
         "options":[  
            {  
               "option":"USPA"
            }
         ]
    }
]

因此下一行代码this.selecto = this.selectq.options;this.selecto设为{{1},而undefinedthis.selectq.options,因为undefined是数组,您需要以使用像this.selectq这样的索引来访问它。因此,将您的代码更改为

this.selectq[0].options

现在,当您管理日志this.selecto = this.selectq[0].options; 时,肯定不会给您selecto