用于加载不同数据的布尔值

时间:2019-02-16 12:29:41

标签: angular ionic4

我正在从事ionic 4项目。我的项目正在从url内容排期中获取数据json。当用户单击按钮时,button boolean在同一页面中显示了不同的数据表单数据URL。当我单击l得到相同的数据时,它不会改变

  Loadearlierflights : boolean = true;

      async getData() {


        const loading = await this.loadingController.create({
          message: 'Loading'
        });
        await loading.present();

          /// flights details for today ///

        if (this.Loadearlierflights===true) {
          this.http.get('xxxxxxx/airport.json?code=bsr', {}, {})
          .then(data => {

            this.test = JSON.parse(data.data);
            const parsed = JSON.parse(data.data);
            this.items = parsed.pluginData.schedule.arrivals.data;
            loading.dismiss()


            console.log(this.items)

          }), err=>{
            this.test =err
            loading.dismiss()
          } 

                   /// flights details for yesterday ///

        }else if (this.Loadearlierflights===false) {
          this.http.get('xxxxxxxx/airport.json?code=bsr&page=-1', {}, {})
          .then(data => {

            this.test = JSON.parse(data.data);
            const parsed = JSON.parse(data.data);
            this.items = parsed.pluginData.schedule.arrivals.data;
            loading.dismiss()


            console.log(this.items)


          }), err=>{
            this.test =err
            loading.dismiss()
          }

        }

html

<ion-button shape="round" (click)="getData()">load earlier flights</ion-button>

<ion-item *ngFor="let item of items" > 
        <table>
            <tbody>
            <tr >
            <td > 
            <div *ngIf="item?.flight.airline.code.icao">
              <img src="/{{item?.flight.airline.code.icao}}_logo0.png"  (error)="handleImgError($event, item)" class="img-logo">

            </div>
            <div *ngIf="!item?.flight.airline.code.icao">
              item?.flight.airline.name
            </div>
              </td>
            <td text-left>{{item?.flight.identification.number.default}}</td>
            <td text-center>{{item?.flight.airport.origin.position.region.city}}</td>
            <td text-right>{{item?.flight.time.scheduled.arrival  * 1000 | date:"h:mma"}}</td>
            </tr>
            <tr >
            <td >&nbsp;</td>
            <td ></td>
            <td text-center>{{item?.flight.aircraft.model.code}}</td>
            <td text-right [ngClass]='item?.flight.status.generic.status.text |Status'>{{item?.flight.status.generic.status.text|Status}}</td>

            </tr>
            </tbody>
            </table>
      </ion-item>

有什么想法吗?

3 个答案:

答案 0 :(得分:1)

if-else中有错误

使用If,如下所示

if (this.Loadearlierflights)
or 
if (this.Loadearlierflights === true)

如下所示使用else-if

else if (this.Loadearlierflights) 
or 
else if (this.Loadearlierflights === false) 

答案 1 :(得分:0)

也许单击模板以指向新方法:

async getEarlierFlightData() {
    this.Loadearlierflights = false;
    await this.getData():
    this.Loadearlierflights = true;
};

答案 2 :(得分:0)

  Loadearlierflights : boolean = true; // should me global

      async getData() {


        const loading = await this.loadingController.create({
          message: 'Loading'
        });
        await loading.present();

          /// flights details for today ///

        if (this.Loadearlierflights===true) {
          this.http.get('xxxxxxx/airport.json?code=bsr', {}, {})
          .then(data => {

            this.test = JSON.parse(data.data);
            const parsed = JSON.parse(data.data);
            this.items = parsed.pluginData.schedule.arrivals.data;
            loading.dismiss()

                // Added this
                this.Loadearlierflights = false;

            console.log(this.items)

          }), err=>{
            this.test =err
            loading.dismiss()
          } 
                   /// flights details for yesterday ///

        }else if (this.Loadearlierflights===false) {
          this.http.get('xxxxxxxx/airport.json?code=bsr&page=-1', {}, {})
          .then(data => {

            this.test = JSON.parse(data.data);
            const parsed = JSON.parse(data.data);
            this.items = parsed.pluginData.schedule.arrivals.data;
            loading.dismiss()


            console.log(this.items)


          }), err=>{
            this.test =err
            loading.dismiss()
          }

        }

让我知道它是否有效。