Angular ngFor 将字符串数组视为一个字符串

发布于 2025-01-14 03:37:04 字数 1714 浏览 5 评论 0原文

我有一个弹出对话框,它从后端接收字符串列表。我想使用 ngFor 将每个字符串打印为列表项。但是,当弹出对话框时,整个数组将显示为一个串联字符串。

needs-dialog.component.ts

import { Component, Inject, OnInit } from '@angular/core';
import {MAT_DIALOG_DATA} from '@angular/material/dialog';
import { MatDialogRef} from "@angular/material/dialog";

@Component({
  selector: 'app-needs-dialog',
  templateUrl: './needs-dialog.component.html',
  styleUrls: ['./needs-dialog.component.css']
})
export class NeedsDialogComponent implements OnInit {

  needs!: String[];

  constructor( private dialogRef: MatDialogRef<NeedsDialogComponent>, @Inject(MAT_DIALOG_DATA) data) 
  {
    console.log("logging data:")
    console.log(data); 
    this.needs=data 
    console.log("logging needs array in NeedsDialogComponent:");
    console.log(this.needs);
  }

  ngOnInit(): void {
  }

  close() {
    
    this.dialogRef.close();
  }

}

needs-dialog.component.html

<h2 mat-dialog-title>Szükségletek</h2>
<mat-dialog-content >
<ul *ngFor="let value of needs; index as i" >
<li>{{needs}}</li>
</ul>
</mat-dialog-content>
<mat-dialog-actions>
  <button mat-button mat-dialog-close>Cancel</button>
</mat-dialog-actions>

**使用方法打开matdialog:**

openDialog(dogid:any): void {
    

    this.matdialog.open(NeedsDialogComponent, {data:this.getDogNeeds(Number(dogid)),width: '500px',
    height: '500px'});
  }

对话框日志记录的控制台输出

对话框窗口

I have a popup dialog, which recieves a list of strings from the backend. I want to print every string as a list item, using ngFor. But when the dialog pops up, the whole array is shown as one concatenated string.

needs-dialog.component.ts

import { Component, Inject, OnInit } from '@angular/core';
import {MAT_DIALOG_DATA} from '@angular/material/dialog';
import { MatDialogRef} from "@angular/material/dialog";

@Component({
  selector: 'app-needs-dialog',
  templateUrl: './needs-dialog.component.html',
  styleUrls: ['./needs-dialog.component.css']
})
export class NeedsDialogComponent implements OnInit {

  needs!: String[];

  constructor( private dialogRef: MatDialogRef<NeedsDialogComponent>, @Inject(MAT_DIALOG_DATA) data) 
  {
    console.log("logging data:")
    console.log(data); 
    this.needs=data 
    console.log("logging needs array in NeedsDialogComponent:");
    console.log(this.needs);
  }

  ngOnInit(): void {
  }

  close() {
    
    this.dialogRef.close();
  }

}

needs-dialog.component.html

<h2 mat-dialog-title>Szükségletek</h2>
<mat-dialog-content >
<ul *ngFor="let value of needs; index as i" >
<li>{{needs}}</li>
</ul>
</mat-dialog-content>
<mat-dialog-actions>
  <button mat-button mat-dialog-close>Cancel</button>
</mat-dialog-actions>

**opening matdialog with method: **

openDialog(dogid:any): void {
    

    this.matdialog.open(NeedsDialogComponent, {data:this.getDogNeeds(Number(dogid)),width: '500px',
    height: '500px'});
  }

console output from dialog logging

dialog window

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南七夏 2025-01-21 03:37:04

根据您的记录,数据是一个二维数组,其中您想要的列表作为第一个元素。

this.needs = data[0];

应该可以工作,但似乎您的 getDogNeeds() 函数或您的后端以错误的格式返回数据。

By your logging, the data is a 2D array with the list you want as the first element.

this.needs = data[0];

should work, but it seems like either your getDogNeeds() function or your back end is returning data in the wrong format.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文