1

我正在开发我的网络应用程序,但遇到了问题。

我有一个包含多个值的数组,我想在前端显示为列表或类似的东西。

app.component.ts

在这个函数中,我将字符串中的标签拆分为一个数组

splitTags() {
  if (this.data.tags != null) {
    var tag = this.data.tags.split(";")
    console.log(tag)
  }
}

ngOnInit() {
  this.splitTags()
}

app.component.html

在这里我想在列表中显示标签

<li *ngFor="let tag in tags">
  {{ tag }}
</li>

但是什么也没有出现,如果我在控制台中看到这些值也是如此。

4

1 回答 1

1

您需要创建一个属性来保存split结果

tags:any[]; // 1️⃣

splitTags() {
  if (this.data.tags != null) {
    this.tags = this.data.tags.split(";"); // 2️⃣
    console.log(this.tags)
  }
}

ngOnInit() {
  this.splitTags()
}


模板

<li *ngFor="let tag of tags">
  {{ tag }}
</li>
于 2020-04-08T08:11:32.050 回答