typescript 为什么是   不工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46223593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:53:22  来源:igfitidea点击:

why is   not working

htmlangulartypescriptecmascript-6

提问by SONGSTER

I want space added between firstname and lastname where   are added. but when i run the code it doesnt adds space. I also tried adding tab sapce as well but its not rendering correctly. The character set is set to utf-8 as can be seen in the attached html

我想在名字和姓氏之间添加空格。但是当我运行代码时,它不会增加空间。我也尝试添加 tab sapce 但它没有正确呈现。字符集设置为 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>

回答by loganfsmyth

Using HTML in your title does not make sense. If you wanted to use &nbsp;then you'd have to render the title as HTML for it to render properly, but that also means if a user had any HTML in their name, it would be an issue. If I register for your site with FirstName: "a <b>name</b>"then it would render as bold alongside the spaces.

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

Instead you should leave it as normal text, and put an actual non-breaking space character in your code, using JS unicode escapes, instead of HTML escapes, e.g. use \u00A0

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

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

回答by Michael

I'm assuming this is Angular.

我假设这是 Angular。

the spaces are not printing because &nbsp;should be rendered as HTML.

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

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

Plunker

普朗克

回答by amal

What if you simply add the space like this?

如果您只是像这样添加空格怎么办?

this.title = `First Name is: ${this.firstName} Last Name is: ${this.lastName}`;

(Notice the space between ...firstName}and Last...)

(注意之间的空间...firstName}Last...