簡體   English   中英

為什么不起作用

[英]why is   not working

我要在名字和姓氏之間添加空格。 但是當我運行代碼時,它不會增加空間。 我也嘗試添加制表符,但不能正確顯示。 字符集設置為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> 

在標題中使用HTML沒有意義。 如果您想使用&nbsp; 那么您就必須將標題顯示為HTML才能正確顯示,但這也意味着,如果用戶的名稱中包含任何HTML,那將是一個問題。 如果我使用FirstName: "a <b>name</b>"注冊您的網站,那么它將在空格旁邊顯示為粗體。

相反,您應該將其保留為普通文本,並使用JS unicode轉義而不是HTML轉義(例如,使用在代碼中放置實際的不間斷空格字符

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

我假設這是Angular。

空格未打印,因為&nbsp; 應該呈現為HTML。

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

柱塞

如果您只是這樣添加空間怎么辦?

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

(注意...firstName}Last...之間的空格)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM