javascript - Angular 将 HTML 响应数据显示到视图中
问题描述
您好,我是 Angular 的新手,我无法弄清楚在下面的组件中显示 html 响应数据是我的代码,请有人帮忙。谢谢
data.Service.ts(这里我调用一个返回一些 html 的 API 服务)
getLibrary() {return this.http.get('http://localhost:8000/api/user/library');}
库.component.ts
import { Component, OnInit } from '@angular/core';
import { DataService } from '../data.service';
import { Observable } from 'rxjs';
@Component({
selector: 'app-library',
templateUrl: './library.component.html',
styleUrls: ['./library.component.scss']
})
export class LibraryComponent implements OnInit {
library: any;
constructor(private data: DataService) { }
ngOnInit() {
this.data.getLibrary().subscribe(
data => this.library = data
);
}
}
库.component.html
{{library}}
错误
core.js:14576 ERROR
HttpErrorResponse {headers: HttpHeaders, status: 200, statusText: "OK", url: "http://localhost:8000/api/user/library", ok: false, …}
error: {error: SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>) at XMLHttp…, text: "<div style="display:block" class="libContainer lib…
↵ </div>
↵
↵
↵</div>
↵
↵
↵
↵
↵
↵
↵
↵
↵"}
headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}
message: "Http failure during parsing for http://localhost:8000/api/user/library"
name: "HttpErrorResponse"
ok: false
status: 200
statusText: "OK"
url: "http://localhost:8000/api/user/library"
__proto__: HttpResponseBase
解决方案
返回的数据类型是文本,您可以像这样使用
ngOnInit() {
this.data.getLibrary().subscribe(
(data:(any)) => this.library = data
);
在视野中
<div [innerHTML]="library"></div>
推荐阅读
- lsf - 如何在 lsf 中设置最大运行作业数
- java - 每类单例,防止额外的实例
- docker - 来自守护进程的错误响应:mob-someserver-L-1:5000/appdynamicsagent:B40 的清单未找到:清单未知:清单未知
- sql - PL/SQL 查询中的 NULL 处理增强
- ssas - SSAS 表格 DMV 获取表的源查询
- c# - 使用 App.Config 派生要使用的类/变量
- javascript - JavaScript:关于从字符串到整数的转换方式的性能
- python-3.x - 我正在尝试使用 'IF' 和 'OR' 函数在新列中具有值
- postgresql - 如何通过 graphql 和 typeorm 更新或创建实体(有关系)
- flutter - 问题 SingleChildScrollView 与 PageView 颤动