【问题标题】:Angular Identifier 'playerType' is not defined. '' does not contain such a member未定义角度标识符“playerType”。 '' 不包含这样的成员
【发布时间】:2023-03-17 09:21:01
【问题描述】:

我正在开发一个 ionic 应用程序,我在 HTML 文件中使用 *ngFor="let playerValue of players" 获取值

但是在插值{{playerValue.playerPhoto}}playerValue.playerType 时,我在 html 中使用 playerValue 的属性(如 playerPhoto 或 playerType)时出错 例如:-

<img src={{playerValue.playerPhoto}}></button> 

<div *ngSwitchCase="playerValue.playerType">

错误信息是-:

[Angular] 标识符“playerType”未定义。 '' 做 不包含这样的成员

[Angular] 标识符“playerPhoto”未定义。 '' 做 不包含这样的成员

我得到错误的html

<ion-content no-padding>
  <div class="create-team-section">
    <ion-list class="team-create-list">
      <div [ngSwitch]="playerType" *ngFor="let playerValue of players |
          search : terms| sort: {property: column, order: order}; let i = index">
        <div *ngSwitchCase="playerValue.playerType" ngSelected="selected">
          <ion-item [class.active]="playerValue.isSelected? 'active' : null">
            <ion-grid no-padding>
              <!-- <a (click)="playerInfo()"><img src={{playerValue.playerPhoto}}></a> -->
              <ion-row align-items-center [class.shake]="shake==playerValue.playerUid ? 'shake' : null">
                <ion-col col-3>
                  <!-- <div class="create-team-imge" >
                      <img src={{playerValue.playerPhoto}}>
                    </div> -->
                  <div class="create-team-imge">
                    <ion-avatar item-star no-line>
                      <button (click)="playerinfo(i)"><img src={{playerValue.playerPhoto}}></button>
                    </ion-avatar>
                    <ion-badge>
                      <ion-icon name="information"></ion-icon>
                    </ion-badge>
                  </div>
                </ion-col>
                <ion-col col-md-9 col-9 (click)="setClickedRow(i)">
                  <ion-row align-items-center>
                    <ion-col col-md-8 col-7>

                      <p>{{playerValue.playerName}}</p>
                      <p>Selected By {{playerValue.selectedBy}}</p>
                      <p class="country" >{{playerValue.teamName}}</p>
                      <p class="points"> Points:
                        <span>{{playerValue.totalPoint}}</span>
                      </p>
                    </ion-col>
                    <ion-col col-md-4 col-5 class="cradit">
                      <p>
                        <span>{{playerValue.playerCreditPoint}}</span>
                      </p>
                      <button ion-button icon-only>
                        <ion-icon [name]="playerValue.isSelected? 'close' : 'ios-checkmark'"></ion-icon>
                      </button>
                    </ion-col>
                  </ion-row>
                </ion-col>
              </ion-row>
            </ion-grid>
          </ion-item>
        </div>
      </div>
    </ion-list>
  </div>

</ion-content>

ts 文件

export class CreateteamPage {

  players: any = [];

【问题讨论】:

  • 也许先尝试删除 *ngFor 上的 '[ngSwitch]="playerType",因为它似乎正在使用您的播放器属性之一。
  • 你输入{{playerValue | json}}时会显示什么?
  • playerValue是如何定义的?
  • @selemmn 它以 { "key" : "value" , "key" : "value" } 格式正确出现
  • @DelwynPinto 是在json中定义的

标签: angular ionic-framework ionic2 ionic3


【解决方案1】:

尝试创建这样的界面

export interface Players {
 playerType: string;
 playerPhoto: string;
}

在你的 ts 文件中:

import { Players } from './models/players' // path to the interface

export class CreateteamPage {

 players: Players[];

 // rest of the code
}

【讨论】:

  • 错误已修复,但由于创建界面,我收到另一个错误 [ts] File 'd:/premier 11/PreUI11/PreUI11/PUI11/src/interface/players.ts' 不是模块。 [2306]
  • 停止服务器,重新编译运行。好像找不到新创建的文件。
【解决方案2】:

试试安全参数?

<img src={{playerValue?.playerPhoto}}></button> 

<div *ngSwitchCase="playerValue?.playerType">

【讨论】:

    猜你喜欢
    • 2021-04-12
    • 2018-05-08
    • 1970-01-01
    • 2021-09-02
    • 2018-02-18
    • 2018-10-06
    • 1970-01-01
    • 2021-03-20
    • 1970-01-01
    相关资源
    最近更新 更多