【问题标题】:How to render html from server in angular 6 [closed]如何以角度 6 从服务器呈现 html [关闭]
【发布时间】:2019-01-12 11:59:56
【问题描述】:
【问题讨论】:
标签:
javascript
angular
angular6
angular-universal
angular-seo
【解决方案1】:
这在 Angular 中是不可能的。您只能使用 [innerHTML] 在模板中渲染纯 HTML,但是您不能以角度方式与它交互。
最好的做法是与服务器通信(使用 POST/GET 请求...),这将向您发送回您文章的序列化“JSON”数据。然后,您可以使用角度组件渲染从服务器接收到的未序列化 JSON 数据。
您可能想看看 REST 应用程序。 :)
【解决方案2】:
您可以使用Meta Service 从您的 API 更新您的元数据
online example
import { Component } from '@angular/core';
import { Meta } from '@angular/platform-browser';
import { HttpClient } from '@angular/common/http';
import { of } from 'rxjs';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
name = 'Angular';
constructor(
private meta: Meta,
private http: HttpClient
) { }
getMetaData$() {
const isDevMode = true;
if (isDevMode) {
const mockMetas = {
['twitter:card']: 'summary',
'title': 'myTitle',
'description': 'myDesc'
}
return of(mockMetas);
} else {
return this.http.get('/?url=function-returning-other-functions-in-javascript')
}
}
ngOnInit() {
this.getMetaData$().subscribe((metas) => {
for (const key of Object.keys(metas)) {
this.meta.updateTag(
{ name: key, content: metas[key] },
);
}
})
}
}