我想在添加的名字和姓氏之间添加空格。但是当我运行代码时,它不会增加空间。我也尝试添加选项卡空间,但它无法正确呈现。字符集设置为 utf-8,如附件 html 中所示
export class AppComponent implements OnInit {
firstName: string;
lastName: string;
title: string;
clicked: boolean;
ngOnInit() {
this.firstName = `John`;
this.lastName = `Doe`;
}
printDetails(frstnm: HTMLInputElement, lstnm: HTMLInputElement, event: any): void {
this.firstName = frstnm.value || this.firstName;
this.lastName = lstnm.value || this.lastName;
this.title = `First Name is: ${this.firstName} Last Name is: ${this.lastName}`;
event.preventDefault();
this.clicked = true;
}
isVisible() {
const classes = {
display : this.clicked
}
return classes;
}
}
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>MyApp</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
<app-root></app-root>
</body>
</html>
原文由 SONGSTER 发布,翻译遵循 CC BY-SA 4.0 许可协议
在标题中使用 HTML 没有意义。如果您想使用
那么您必须将标题呈现为 HTML 才能正确呈现,但这也意味着如果用户的 名称 中包含任何 HTML,这将是一个问题。如果我使用FirstName: "a <b>name</b>"
注册您的网站,那么它将在空格旁边呈现为粗体。相反,您应该将其保留为普通文本,并在您的代码中放置一个实际的不间断空格字符,使用 JS unicode 转义,而不是 HTML 转义,例如使用
\u00A0