Angular 页面显示为空,而组件有许多元素要显示在屏幕上

Angular 页面显示为空,而组件有许多元素要显示在屏幕上,angular,Angular,我们正在尝试将数据对象中的内容放置在行和列中。然而,当我们检查错误时,屏幕显示为空,控制台上显示为未捕获错误,但我们很困惑,无法确定哪些行是完全错误的,以及如何修复它们。键入脚本部分就可以了。似乎所有的错误都在组件的HTML部分。代码如下: <div class="container justify-content-center"> <div class="row row row-centered justify-content-center" *ngFor="let rest

我们正在尝试将数据对象中的内容放置在行和列中。然而,当我们检查错误时,屏幕显示为空,控制台上显示为未捕获错误,但我们很困惑,无法确定哪些行是完全错误的,以及如何修复它们。键入脚本部分就可以了。似乎所有的错误都在组件的HTML部分。代码如下:

<div class="container justify-content-center">
<div class="row row row-centered justify-content-center" *ngFor="let restaurant of restaurants; let i = index" *ngIf="i % 3 == 0">
      <div class="col-md-3 centered col-md-offset-1">
        <span>
        <img
          [src]="restaurant.imagePath"
          alt="{{ restaurant.name }}"
          class="img-responsive"
          style="max-height: 300px; width:100%;">
      </span>
          <h2>{{restaurant.name}} </h2>
          <h2>{{restaurant.category}} </h2>
          <p>{{restaurant.description}} </p>
         </div>
        <div class="col-md-3 centered col-md-offset-1">
        <span>
        <img *ngIf="restaurants[$index + 1].imagePath != null" [src]="restaurant[i +
              1].imagePath"
          class="img-responsive"
          style="max-height: 300px; width:100%;">
      </span>
          <h2 *ngIf="restaurants[$index + 1].name != null">{{restaurants[i +
              1].name}} </h2>
          <h2*ngIf="restaurants[$index + 1].category != null">{{restaurants[i +
              1].category}} </h2>
              <p *ngIf="restaurants[$index + 1].description != null">{{restaurants[i +
              1].description}} </p>

      </div>
         <div class="col-md-3 centered col-md-offset-1">
        <span>
        <img *ngIf="restaurants[$index + 2].imagePath != null" [src]="restaurant[i +
              2].imagePath"
          class="img-responsive"
          style="max-height: 300px; width:100%;">
      </span>
          <h2*ngIf="restaurants[$index + 2].name != null">{{restaurants[i +
              2].name}} </h2>
          <h2*ngIf="restaurants[$index + 2].category != null">{{restaurants[i +
              2].category}} </h2>
              <p *ngIf="restaurants[$index + 2].description != null">{{restaurants[i +
              2].description}} </p>
                       </div>
      </div>

  </div>
</div>

{{餐厅名称}
{{餐厅.类别}
{{餐厅.说明}

{{餐厅[i]+ 1] .name} {{餐厅[i]+ 1] .category} {{{餐馆[i]+ 1] .description}

{{餐厅[i]+ 2] .name} {{餐厅[i]+ 2] .category} {{{餐馆[i]+ 2] .description}


怎么了?我们如何修复这些错误并使其在浏览器上显示组件?

在HTML模板中,您在同一元素上使用了ngForngIf,但根据,每个主体元素只能使用一个结构指令

您可以根据使用以下语法,并在ng模板内的div上使用ngIf

<ng-template ngFor let-item [ngForOf]="items" let-i="index" [ngForTrackBy]="trackByFn">
  <div>...</div>
</ng-template>

...
对于柱基布局,您可以为您的柱创建一个组件,并将餐厅数据作为输入传递给它。在html中使用此新组件三次,如下所示:

// Component
public items = new Array(Math.round(this.restaurents.length/3));

//Template

<ng-template ngFor let-item [ngForOf]="items" let-i="index" [ngForTrackBy]="trackByFn">
  <my-col [data]="itmes[i*3]" ngIf="itmes[i*3]">...</my-col>
  <my-col [data]="itmes[i*3 + 1]" ngIf="itmes[i*3 + 1]">...</my-col>
  <my-col [data]="itmes[i*3 + 2]" ngIf="itmes[i*3 + 2]">...</my-col>
</ng-template>
//组件
公共项=新数组(Math.round(this.restaurents.length/3));
//模板
...
...
...

在HTML模板中,您在同一元素上使用了ngForngIf,但根据,每个主体元素只能使用一个结构指令

您可以根据使用以下语法,并在ng模板内的div上使用ngIf

<ng-template ngFor let-item [ngForOf]="items" let-i="index" [ngForTrackBy]="trackByFn">
  <div>...</div>
</ng-template>

...
对于柱基布局,您可以为您的柱创建一个组件,并将餐厅数据作为输入传递给它。在html中使用此新组件三次,如下所示:

// Component
public items = new Array(Math.round(this.restaurents.length/3));

//Template

<ng-template ngFor let-item [ngForOf]="items" let-i="index" [ngForTrackBy]="trackByFn">
  <my-col [data]="itmes[i*3]" ngIf="itmes[i*3]">...</my-col>
  <my-col [data]="itmes[i*3 + 1]" ngIf="itmes[i*3 + 1]">...</my-col>
  <my-col [data]="itmes[i*3 + 2]" ngIf="itmes[i*3 + 2]">...</my-col>
</ng-template>
//组件
公共项=新数组(Math.round(this.restaurents.length/3));
//模板
...
...
...

我认为这将非常有效:

<div class="container-fluid">
  <div *ngFor="let restaurant of restaurants; let i = index" >
    <div class="row row-centered justify-content-center" *ngIf="i % 3 === 0">
              <div class="col-md-3 centered">
           <img
          [src]="restaurants[i].imagePath"
          class="img-responsive"
          style="max-height: 300px; width:100%;"/>
          <h2>{{restaurants[i].name}} </h2>
          <h3>{{restaurants[i].category}} </h3>
          <p>{{restaurants[i].description}} </p>
      </div>
      <div class="col-md-3 centered">
       <img
          [src]="restaurants[i + 1].imagePath"
          class="img-responsive"
          style="max-height: 300px; width:100%;"/>
          <h2>{{restaurants[i + 1].name}} </h2>
          <h3>{{restaurants[i + 1].category}} </h3>
          <p>{{restaurants[i + 1].description}} </p>
      </div>
      <div class="col-md-3 centered">
       <img
          [src]="restaurants[i+2].imagePath"
          class="img-responsive"
          style="max-height: 300px; width:100%;"/>
          <h2>{{restaurants[i+2].name}} </h2>
          <h3>{{restaurants[i+2].category}} </h3>
          <p>{{restaurants[i+2].description}} </p>
        </div>
    </div>
  </div>
</div>

{{餐厅[i].name}
{{餐厅[i].类别}
{{餐厅[i].说明}

{{餐厅[i+1].名称} {{餐厅[i+1]。类别} {{餐厅[i+1]。说明}

{{餐厅[i+2].名称} {{餐厅[i+2]。类别} {{餐厅[i+2]。说明}


我认为这将非常有效:

<div class="container-fluid">
  <div *ngFor="let restaurant of restaurants; let i = index" >
    <div class="row row-centered justify-content-center" *ngIf="i % 3 === 0">
              <div class="col-md-3 centered">
           <img
          [src]="restaurants[i].imagePath"
          class="img-responsive"
          style="max-height: 300px; width:100%;"/>
          <h2>{{restaurants[i].name}} </h2>
          <h3>{{restaurants[i].category}} </h3>
          <p>{{restaurants[i].description}} </p>
      </div>
      <div class="col-md-3 centered">
       <img
          [src]="restaurants[i + 1].imagePath"
          class="img-responsive"
          style="max-height: 300px; width:100%;"/>
          <h2>{{restaurants[i + 1].name}} </h2>
          <h3>{{restaurants[i + 1].category}} </h3>
          <p>{{restaurants[i + 1].description}} </p>
      </div>
      <div class="col-md-3 centered">
       <img
          [src]="restaurants[i+2].imagePath"
          class="img-responsive"
          style="max-height: 300px; width:100%;"/>
          <h2>{{restaurants[i+2].name}} </h2>
          <h3>{{restaurants[i+2].category}} </h3>
          <p>{{restaurants[i+2].description}} </p>
        </div>
    </div>
  </div>
</div>

{{餐厅[i].name}
{{餐厅[i].类别}
{{餐厅[i].说明}

{{餐厅[i+1].名称} {{餐厅[i+1]。类别} {{餐厅[i+1]。说明}

{{餐厅[i+2].名称} {{餐厅[i+2]。类别} {{餐厅[i+2]。说明}


请在问题中包含错误。如果可能的话,还可以尝试通过通常的二分法过程将html代码最小化到最低限度。当标记已被另一个标记关闭时,可能会发生这种情况。有关详细信息,请参见(“{{restaurants[i+1].category}}}[ERROR->]{{restaurants[i+”):@LucaCappelletti这是错误消息。请将其包含在问题中。请将错误包含在问题中。如果可能的话,还可以尝试通过通常的二分法过程将html代码最小化。意外的结束标记“h2”。当该标记已被另一个标记关闭时,可能会发生这种情况。有关更多信息,请参阅(“{restaurants[i+1]。category}}[ERROR->]{{restaurants[i+”):@LucaCappelletti这是错误消息,请将其包含在问题中。那么如何实现上述逻辑?问题是行需要ngfor,因为每行有3列。该项目基于bootstrap 4。我认为此解决方案无法使用它。那么如何将此逻辑实现为des上面提到过吗?问题是行需要ngfor,因为每行有3列。该项目基于bootstrap 4。我认为这个解决方案不适用于它。非常感谢。最后,我得到了一个解决方案。它很有效!!非常感谢。最后,我得到了一个解决方案。它很有效!!