html - Angular 8 过滤器 : preserve line breaks on text area input

标签 html angular input filter pipe

我尝试使用 angular 8 制作一个过滤器,以在我的文本区域输入中保留文本换行符,文本输入将放置在图像上方:

<div class="form-group col-10">
          <span class="badge badge-theme mb-3">Message personnalisé</span>
          <textarea class="form-control" id="exampleFormControlTextarea1" formControlName="personalizedMessage"
            rows="6"></textarea>

        </div>
<div class="col-6" style="margin-top: 35px;">
      <div class="row">
        <div class="col-12 card-container">
          <img src={{displayedImage}} alt="">
          <div class="message">
            <p ng-bind-html="personalizedMessage | linebreaks">{{giftCardForm.controls['personalizedMessage'].value}}</p>
          </div>
        </div>
      </div>
    </div>

管道
import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'linebreaks'
})
export class LinebreaksPipe implements PipeTransform {

  transform(text: any): any {
    return text.replace(/\n/g, "<br>");
  }

}

我是 angular 的新手,我不确定管道是否写得好!有人可以帮忙吗!

最佳答案

Angular 的工作示例,带有 react 形式 , 如果你想使用 模板驱动表单 , 使用代替 formControlName="message" ---> [(ngModel)]="message"

  • 有一个 文本区域输入
  • 的位置

    示例
     <textarea placeholder="Message" formControlName="message" rows="3"></textarea>
    
  • 在哪里展示
  • <div class="message" [innerHTML]="message">
  • 使用
  • 为容器设计样式
    .message { white-space: pre-wrap;}

    关于html - Angular 8 过滤器 : preserve line breaks on text area input,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58949994/

    相关文章:

    html - 将URL作为src传递时的HTML5音频元素关闭连接

    angular - 我如何在 angular 中使用品牌 fontawesome 图标

    c++ - 从文件c++中将单个字符读入变量

    python - 将数字输入数组

    javascript - 客户端与服务器端图像压缩

    html - 单击时删除元素周围出现的蓝色边框

    Angular 2 - Angularfire 2 身份验证状态

    angular - 如何防止 Angular ngFor 将数据追加到当前列表?

    php - 将 android 连接到队列应用程序的数据库

    css - 我无法在 Bootstrap 元素中编辑我的 custom.css