I am looking for a way to show markup document texts from a file in asset folder in html as a pop-up in Angular

huangapple go评论63阅读模式
英文:

I am looking for a way to show markup document texts from a file in asset folder in html as a pop-up in Angular

问题

以下是翻译好的部分:

Text File Data ->

<b>测试</b>

这是我尝试的 Angular HTML 片段。
Code

<embed src=".\assets\docs\about\BRANCH_MASTER.txt">-->
        
        <object data=".\assets\docs\about\BRANCH_MASTER.txt">
          不支持
        </object>

Output

<b>测试</b>

问题是标记标签未序列化。它显示为原样。
是否有一种方法可以访问资产文件内容,并在 HTML 中显示它们?

英文:

Text File Data ->

<b>Test</b>

Here is the angular html snippet i tried.
Code

<embed src=".\assets\docs\about\BRANCH_MASTER.txt">-->
        
        <object data=".\assets\docs\about\BRANCH_MASTER.txt">
          Not supported
        </object>

Output

<b>Test</b>

Problem is markup tags not serializing. It is showing as it is.
Is there any way to access asset file content. And Show them in html?

答案1

得分: 1

你需要完成两个任务:

首先,从文件中检索数据。使用 HttpClient 发送 HTTP GET 请求以检索 test.txt 文件的内容。将 responseType 设置为 'text' 以确保将响应视为纯文本。

content = "";
constructor(private http: HttpClient) {}

ngOnInit() {
  this.http.get('assets/test.txt', {responseType: 'text'})
    .subscribe((data) => this.content = data);
}

其次,将数据绑定到变量并使用 innerHTML 在模板中显示它。这将在 <div> 元素内呈现文件的内容作为 HTML。

<div [innerHTML]="content"></div>
英文:

You have to do two things

First, retrieve data from the file. Use HttpClient to make an HTTP GET request to retrieve the content of the file test.txt. The responseType is set to 'text' to ensure that the response is treated as plain text.

  content = &quot;&quot;;
  constructor(private http: HttpClient) {}

  ngOnInit() {
    this.http.get(&#39;assets/test.txt&#39;, {responseType: &#39;text&#39;})
      .subscribe((data) =&gt; this.content = data);
  }

Second, bind the data to a variable and use innerHTML to display it in the template.This will render the contents of the file as HTML within the <div> element.

&lt;div [innerHTML]=&quot;content&quot;&gt;&lt;/div&gt;

huangapple
  • 本文由 发表于 2023年6月19日 12:53:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76503686.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定