我有我的应用程序组件类,我想在其中添加新组件.然后,我将新组件的 Select 器添加到主类模板中.

 `import {CountryCapitalComponent} from "./app.country";
  @Component({
   selector: 'app-root',
   template: `
   <h2 class="counter">Title</h2>
 <section>
  <app-country-capital [data]="{ 'Germany': 'Berlin', 'Azerbaijan': 'Baku' }"></app-country-capital>
</section>
`,
standalone: true,
imports: [
CountryCapitalComponent
],
  styles: [`
.counter {
  font-size: 1.5rem;
  margin-bottom: 10px;
}

`]
})
export class AppComponent {

` 我制作了另一个部件,看起来像这样

    @Component({
  selector: 'app-country-capital',
  template: `
    <div *ngFor="let pair of shuffledPairs">
                  {{ pair.display }}
      
    </div>
  `,
  standalone: true,
  imports: [
    NgStyle
  ],
  styles: []
})
export class CountryCapitalComponent implements OnInit {
  @Input() data: { [country: string]: string; } | string ="{ 'Germany': 'Berlin', 'Azerbaijan': 'Baku' }";

 initializePairs() {
let pairs: Pair[];
pairs = Object.entries(this.data).reduce((acc: Pair[], [country, capital]) => {
  return acc.concat(
    {display: country, correct: true, clicked: false},
    {display: capital, correct: false, clicked: false}
  );
}, []);

    // Shuffle the pairs
    this.shuffledPairs = this.shuffleArray(pairs);
  }
  shuffleArray(array: any[]) {
    for (let i = array.length - 1; i > 0; i--) {
      const j = Math.floor(Math.random() * (i + 1));
      [array[i], array[j]] = [array[j], array[i]];
    }
    return array;
  }

 
}

当我运行代码时,我没有获得CountryCapitalComponent的视图.HTMLElemt如下所示

<app-country-capital _ngcontent-ng-c1124832886="" ng-reflect-data="[object Object]"><!--container--></app-country-capital>

如何获取CountryCapitalComponent的视图.正如我所理解的,我正确地将属性数据绑定到子组件.我错过了什么.

推荐答案

@Component({
  selector: 'app-country-capital',
  standalone: true,
  imports: [NgFor],
  template: `
    <div *ngFor="let pair of shuffledPairs">
      {{ pair.display }}
    </div>
  `,
})

别忘了在ngOnInit个生命周期方法中调用此方法

ngOnInit() {
    this.initializePairs();
  }

以下是代码示例-Stackblitz

Html相关问答推荐

如何防止下拉菜单内的Bootstrap列表行包装?

传单自定义标记(divIcon)html/css不适用

防止SVG边框半径zoom

如何在div中淡入和淡出渐变背景?

垂直页眉,每行只显示一个使用css的字母

如何创建带有粘性列和标题的网格?

未知高度正在应用于前标记

如何设置弹性盒子容器的具体大小?

如何创建剪切到路径的循环文本字幕?

轨道上的居中范围滑块拇指(Webkit)

我正在try 向列表中的图像添加悬停,以放大图像并将div的不透明度设置为1而不是0

如何在CSS伪类函数中使用复合 Select 器:host-context()

如何将背景与之前的内容正确对齐

透明渐变凹矩形

文本溢出:省略号不适用于 flexbox

涉及短代码时如何在页面上定位元素?

自定义进度条 Html 和 CSS 布局

在中心而不是边界处填充 svg 的背景

当我希望它只横向滚动时,可滚动菜单也会上下移动

在部分而不是正文中定义页面宽度