在我的Angular应用程序中,我使用CSS开发了一个具有两列的布局组件.在这个布局组件中,我使用ng-content定义了旁边和主要内容的占位符.

旁边和主要部分的数据从服务器获取.在加载阶段,设置loading标志以指示正在获取数据.成功检索数据后,将从观察对象中返回isSuccess标志和响应.

为了模拟这种行为,我使用RxJS中的of操作符创建了一个模拟data$观察对象.该可观察结果包括模拟同步数据检索过程的延迟.

布局没有正确呈现旁白和主要内容.出现这个问题是因为ng-content指令直接期望属性asidemain,但它们深深地嵌套在ng-container中.

问题是,可以修改此设置以正确呈现内容吗?

以下是修改后的代码:

import { CommonModule } from '@angular/common';
import { Component } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
import { delay, of, startWith } from 'rxjs';
import 'zone.js';

@Component({
  selector: 'layout',
  standalone: true,
  template: `
    <div class="container">
      <aside>
        <ng-content select="[aside]"></ng-content>
      </aside>
      <main>
        <ng-content select="[main]"></ng-content>
      </main>
    </div>
  `,
  styles: [`.container {display:grid; grid-template-columns: 1fr 2fr} `],
})
export class LayoutComponent {}

@Component({
  selector: 'app-root',
  standalone: true,
  imports: [LayoutComponent, CommonModule],
  template: `
    <layout>
      <ng-container *ngIf="data$ | async as result">
        <div *ngIf="result.loading">loading...</div>
        <ng-container *ngIf="result.isSuccess">
          <div aside>aside content: {{ result.response.aside | json }}</div>
          <div main>main content: {{ result.response.main | json }}</div>
        </ng-container>
      </ng-container>
    </layout>
  `,
})
export class App {
  data$ = of<any>({
    response: { aside: [1, 2, 3, 4], main: [10, 12, 13, 14] },
    isSuccess: true,
  }).pipe(delay(1 * 1000), startWith({ loading: true }));
}

bootstrapApplication(App);

这是代码的链接:StackBlitz

推荐答案

ng-content个标签必须出现在顶层,才能在内容投影中可见.

所以我重组了HTML,以便我们获取数据并根据是否满足所有条件有条件地显示内容,然后使用if else (ng-template)我们就可以显示加载部分!

请随时修改代码以满足您的要求!

完整代码:

import { CommonModule } from '@angular/common';
import { Component } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
import { delay, of, startWith } from 'rxjs';
import { endWith } from 'rxjs/operators';
import 'zone.js';
console.clear();

@Component({
  selector: 'layout',
  standalone: true,
  template: `
    <div class="container">
      <aside>
        <ng-content select="[aside]"></ng-content>
      </aside>
      <main>
        <ng-content select="[main]"></ng-content>
      </main>
    </div>
  `,
  styles: [`.container {display:grid; grid-template-columns: 1fr 2fr} `],
})
export class LayoutComponent {}

@Component({
  selector: 'app-root',
  standalone: true,
  imports: [LayoutComponent, CommonModule],
  template: `
    <ng-container *ngIf="data$ | async as result">
      <layout *ngIf="result.isSuccess;else loading">
        <div aside>aside content: {{ result.response.aside | json }}</div>
        <div main>main content: {{ result.response.main | json }}</div>
      </layout>
    </ng-container>
    <ng-template #loading>
      <div>loading...</div>
    </ng-template>
  `,
})
export class App {
  name = 'Angular';

  data$ = of<any>({
    response: { aside: [1, 2, 3, 4], main: [10, 12, 13, 14] },
    isSuccess: true,
  }).pipe(delay(1 * 1000), startWith({ loading: true }));
}

bootstrapApplication(App);

Stackblitz Demo

Javascript相关问答推荐

创建私有JS出口

*ngFor和@代表输入decorator 和选角闭合

如何将连续的十六进制字符串拆分为以空间分隔的十六进制块,每个十六进制块包含32个二元组?

如何在RTK上设置轮询,每24小时

了解Node.js中的EventEums和浏览器中的addEventEums之间的关系

MongoDB中的引用

Spring boot JSON解析错误:意外字符错误

WebRTC关闭navigator. getUserMedia正确

如何控制Reaction路由加载器中的错误状态?

我正在建立一个基于文本的游戏在react ,我是从JS转换.我怎样才能使变量变呢?

如何在Angular17 APP中全局设置AXIOS

如何使用JavaScript拆分带空格的单词

如何在Svelte中从一个codec函数中调用error()?

删除加载页面时不存在的元素(JavaScript)

Reaction useState和useLoaderData的组合使用引发无限循环错误

P5JS-绘制不重叠的圆

如何在尚未创建的鼠标悬停事件上访问和着色div?

如何在Java脚本中并行运行for或任意循环的每次迭代

如何在Highlihte.js代码区旁边添加行号?

如何在Reaction中设置缺省值, Select 下拉列表,动态追加剩余值?