Ionic Error v.context.$implicit is undefined

2019-07-09 08:54发布

问题:

I have the error in my code. The error says like this: v.context.$implicit is undefined

The problem is, sometimes it error, and sometimes it doesn’t… Can somebody explain why it happen…

This my html code:

<ng-container *ngFor="let time of item.timeInfo | keys" >
    <ion-row *ngIf="time == thisDay"> 
      <ion-col col-3 no-padding>
        <ng-container *ngIf="checkTime(); else closeButton">
          <button small ion-button block color="secondary" outline> OPEN </button>
        </ng-container>
        <ng-template #closeButton>
          <button small ion-button block color="danger" outline>Close </button>
        </ng-template>
      </ion-col>
      <ion-col col-9 >
        {{ item.timeInfo[thisDay].open }} - {{ item.timeInfo[thisDay].close }}
      </ion-col>
    </ion-row>
  </ng-container>

Here my json code:

"item":{
   "timeInfo": {
            "Sat": {
                "open": "11:00",
                "close": "21:00"
            },
            "Sun": {
                "open": "9:00",
                "close": "21:30"
            },
            "Mon": {
                "open": "11:00",
                "close": "22:30"
            },
            "Tue": {
                "open": "12:00",
                "close": "22:00"
            },
            "Web": {
                "open": "12:00",
                "close": "22:30"
            },
            "Thu": {
                "open": "9:30",
                "close": "22:30"
            },
            "Fri": {
                "open": "12:30",
                "close": "22:00"
            }
        },
 };

And here my version:

@ionic/cli-utils  : 1.15.2
ionic (Ionic CLI) : 3.15.2

local packages:
@ionic/app-scripts : 3.0.0
Ionic Framework    : ionic-angular 3.7.1

System:
Node : v6.11.2
npm  : 5.4.2
OS   : Windows 8.1

Misc:
backend : pro

Can somebody help me?? Tyvm

回答1:

i can't really help if i don't see you're TS code. The same problem occured to me and my mistake was to try to insert a new data in an array like result[i] = data[i] and not like result.push(data[i]) which is actually correct. Hope it helps !



回答2:

Most of the time, context.$implicit refer in the template to a variable created with a "let" (like "let time of item.info").

The rest of the error ("is undefined") is the classic javascript error message for trying to use a undefined value like an object.

In your case, it probably happen when the variable thisDay is not one of the key of timeinfo. checking the exact line of the error will probably help finding the error in the html.