简体   繁体   English

页面渲染后源中的角空格

[英]Angular whitespaces in source after page has rendered

In an angular application I'm retrieving a list of addresses from a service that returns an object that looks like: 在有角度的应用程序中,我从服务中检索地址列表,该服务返回一个看起来像这样的对象:

{ 
  Flat: "10B",
  BuildingName: "Some Building",
  Line: "10B Some Building Road Town POST CODE",
  Postcode: "POST CODE",
  Street: "Road",
  Town: "Town"
}

Once this data is passed into the controller I need to display the address as a single line with commas. 将这些数据传递到控制器后,我需要用逗号将地址显示为一行。 I use a filter to add the commas (the values of Line is not useful here because it does not contain commas). 我使用过滤器添加逗号( Line的值在这里没有用,因为它不包含逗号)。 Then the data is rendered in the html like so: 然后将数据呈现在html中,如下所示:

<a>{{ address.Flat | joinBy: ',' }} {{ address.BuildingName | joinBy: ',' }} {{ address.BuildingNumber }} etc...</a>

When one of the expressions contains and empty value (eg the address doesn't have a building name) then the ui just omits it so everything looks fine to the user. 当其中一个表达式包含一个空值(例如,地址没有建筑物名称)时,ui只会忽略它,因此用户看起来一切都很好。 But the page source includes a additional whitespace where the empty expression is. 但是页面源中包含一个空白,该空白处是空表达式。

Logging the <a> element shows that it's outerHTML, innerHTML or innerText or text does not have any additional whitespace. 记录<a>元素表明它的externalHTML,innerHTML或innerText或文本没有任何其他空格。

Because there are spaces between the expressions in the html, they are left in there when there is an empty expression. 因为html中的表达式之间有空格,所以当有空表达式时,它们会留在其中。

Is there any way to strip the whitespace from the page source using angular, or should I just use vanilla js and run the string through a regex? 有什么方法可以使用angular从页面源中剥离空格,还是应该只使用vanilla js并通过正则表达式运行字符串?

This was actually a simple fix and staring me right in the face. 这实际上是一个简单的解决方法,直面我。 Empty expressions weren't leaving whitespace, I was adding that to include spaces between address parts. 空表达式不会离开空格,我要添加空格以在地址部分之间包含空格。

I'm already using the joinBy filter that accepts any delimiter so I used this. 我已经在使用可以接受任何定界符的joinBy过滤器,因此我使用了它。 The angular markup now looks like this: 角度标记现在看起来像这样:

<a>{{ address.Flat | joinBy: ', ' }}{{ address.BuildingName | joinBy: ', ' }} {{ address.BuildingNumber | joinBy: ' ' }} {{ address.Street }} etc...</a>

There shouldn't be a comma between building number and street so I just add a space that only renders if the building number exists. 建筑物编号和街道之间不应有逗号,因此我只添加一个仅在建筑物编号存在时才呈现的空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM