英文:
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 = "";
constructor(private http: HttpClient) {}
ngOnInit() {
this.http.get('assets/test.txt', {responseType: 'text'})
.subscribe((data) => 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.
<div [innerHTML]="content"></div>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论