为什么是 不工作

新手上路,请多包涵

我想在添加的名字和姓氏之间添加空格。但是当我运行代码时,它不会增加空间。我也尝试添加选项卡空间,但它无法正确呈现。字符集设置为 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 许可协议

阅读 245
2 个回答

在标题中使用 HTML 没有意义。如果您想使用 &nbsp; 那么您必须将标题呈现为 HTML 才能正确呈现,但这也意味着如果用户的 名称 中包含任何 HTML,这将是一个问题。如果我使用 FirstName: "a <b>name</b>" 注册您的网站,那么它将在空格旁边呈现为粗体。

相反,您应该将其保留为普通文本,并在您的代码中放置一个实际的不间断空格字符,使用 JS unicode 转义,而不是 HTML 转义,例如使用 \u00A0

 this.title = `First Name is: ${this.firstName}\u00A0\u00A0\u00A0Last Name is: ${this.lastName}`;

原文由 loganfsmyth 发布,翻译遵循 CC BY-SA 3.0 许可协议

我假设这是 Angular。

空格未打印,因为 &nbsp; 应呈现为 HTML。

 <div [innerHTML]="title"></div>

笨蛋

原文由 Michael 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题