【发布时间】:2020-04-24 07:58:52
【问题描述】:
export class MessageComponent implements OnInit {
list: Foo[] = [{name: 'Name 1', groupName: 'Group 1'}, {name: 'Name 1', groupName: 'Group 1'}]
message = '';
constructor() { }
ngOnInit() {
this.message = this.confirmRemoval(this.list);
}
confirmRemoval(messageList: Foo[]): string {
return `The following products already belong to a group and will be removed from it: ${messageList.map(v =>
`- ${v.name} belongs to the group ${v.groupName}`).join("<br/>\n")}. Do you wish to continue?`;
}
}
export interface Foo {
name?: string;
groupName?: string;
}
<h3>This is what it looks like:
</h3>
<p>{{message}}</p>
<h3>What I wanted was to see if I could make the message have breaklines where the join is.
我有一个创建字符串的箭头函数。
我将该字符串作为消息传递给创建警报的组件。
所述组件向用户显示消息。
这是我的箭头函数:
export const confirmRemoval = (list: PersonalizedType[]): string =>
'The following products already belong to a group and will be removed from it: ${list
.map(v => `- ${v.name} belongs to the group ${v.groupName}`)
.join(',')}. Do you wish to continue?';
如何让 join 语句在 HTML 中创建一个新行?是否可以仅使用文字 JS 来执行此操作,还是我需要做其他事情?
现在它通过在每条消息之间创建一个逗号来达到所需的目的,但我有点希望它会创建一个新行以提供更好的用户体验。
不确定它是否相关,但我在这个项目上使用 Angular 2+。
在此先感谢并抱歉格式化,我是新来这里提问的。
编辑:忘了提及这一点,但我尝试在 join 方法中使用 '\n' 和 'HTML breakline tag' 但它不起作用。
从一个有效的 stackblitz 链接到一个例子:
https://stackblitz.com/edit/angular-hkyki9?file=src%2Fapp%2Fapp.module.ts
【问题讨论】:
-
请添加显示实际问题的minimal reproducible example,尤其是带有“到创建警报的组件”的部分。
-
我会试试的,一旦我完成了我会更新帖子。
-
我添加了描述链接。
-
minimal reproducible example 必须在问题中包含代码,而不仅仅是在外部站点上。如果 StackBlitz 倒闭或者那个 blitz 被删除,这个问题就没什么用了。请注意,您可能会使用Stack Snippets 在 SO 上提供一个可运行的示例。
-
sn-p 的选项中只有 AngularJS,没有 Angular 2+。我正在使用 Angular 8,我该怎么做才能使它工作?
标签: javascript angular dom