如何在html

时间:2018-07-11 17:41:56

标签: javascript html angular

我需要在我的Angle 4视图(HTML)中显示多行变量。问题是我将HTML标记br /放置在我希望字符串中断并从新行开始的位置,但是br /却显示为文本。我已经尝试过br /,\ r br /,...这些作品都没有。这是文本示例:

文件内容

<br /> xxxxx connects buyers of automotive parts and services with providers of those services. This document outline what information we collect from both the buyers and providers and how we handle this information, our privacy policy.<br />1. Scope of this Privacy Policy:<br /> xxxxxx and its parents, subsidiaries, representatives, affiliates, officers and directors (collectively, 'PuppyDog') values the privacy of individuals who use our application, websites, and related services. This privacy policy (the 'Privacy Policy') explains how we collect, use, and share information from xxxxxxx users (('Users'), comprised of both buyers of automotive parts and services and providers of those services.<br />2. Information We Collect-<br />Personal Information-<br />Buyers Registration Information. When you register as a.......

我应该指出,JSON文件中的变量使用数据绑定在HTML中显示值。如有任何建议,我将不胜感激。

2 个答案:

答案 0 :(得分:1)

使用元素的innerHTML属性在其中添加HTML

<script>
var x = "<br /> xxxxx connects buyers of automotive parts and services with providers of those services. This document outline what information we collect from both the buyers and providers and how we handle this information, our privacy policy.<br />1. Scope of this Privacy Policy:<br /> xxxxxx and its parents, subsidiaries, representatives, affiliates, officers and directors (collectively, 'PuppyDog') values the privacy of individuals who use our application, websites, and related services. This privacy policy (the 'Privacy Policy') explains how we collect, use, and share information from xxxxxxx users (('Users'), comprised of both buyers of automotive parts and services and providers of those services.<br />2. Information We Collect-<br />Personal Information-<br />Buyers Registration Information. When you register as a.......";
function addText(){
 document.getElementById("x").innerHTML = x;
}
</script>
<span id="x"></span>
<br/>
<button onClick="addText()">Fill span with text</button>

使用以下语法:<div [innerHTML]="HTMLstring"></div>

答案 1 :(得分:1)

要使用多行变量,请使用符号`

要使用新字符串,请使用\n

赞:

var text=`\n
 xxxxx connects buyers of automotive parts and services with providers of those services. This document outline what information we collect from both the buyers and providers and how we handle this information, our privacy policy.\n
 1. Scope of this Privacy Policy:\n
 xxxxxx and its parents, subsidiaries, representatives, affiliates, officers and directors (collectively, 'PuppyDog') values the privacy of individuals who use our application, websites, and related services. This privacy policy (the 'Privacy Policy') explains how we collect, use, and share information from xxxxxxx users (('Users'), comprised of both buyers of automotive parts and services and providers of those services.\n
 2. Information We Collect-\n
 Personal Information-\n
 Buyers Registration Information. When you register as a.......
`;

alert(text);