用新行显示内联错误信息

问题描述 投票:0回答:1

我正试图显示内联错误信息。

我的目标是实现这个目标。

错误1

错误2

但它却出现在同一行中,如 。错误1 错误2

我试着用document.createElement("br"),但没有用。

以下是我的代码。

HTML:

  <span *ngIf="ifError()"class="validation_class">
  <p id="inline_error">{{inlineErrorMsgs}}</p></span>

TS:

 ifError(){
 var Errormsg:string;
 var br = document.createElement("br");
 for(var i=0;i<this.messages.length;i++){
 Errormsg = this.messages[i]
 this.inlineErrorMsgs = this.inlineErrorMsgs+Errormsg+br;
 }

我如何添加新行

html angular dom
1个回答
0
投票

HTML

<div *ngIf="errorMessages && errorMessages.length > 0">
<div *ngFor="let d of errorMessages">
<p>
 {{d}}
</p>
</div>
</div>

TS

export class ComponentA {
errorMessages : [] = [];

someMethod(){
this.messages.forEach((j) => {
// here you can add some logic if you want.
errorMessages.push(j);
})
}

请不要直接使用文档对象,或者说尽量避免使用。


1
投票

将错误信息存储在数组中,然后使用ng-for循环显示。

© www.soinside.com 2019 - 2024. All rights reserved.