我有这个代码:
import { Component, ElementRef, Renderer2 } from '@angular/core';
@Component({
selector: 'my-app',
template: '<button (click)="runR()">Run</button>
<div class="testme">
<div class="somediv">
<div class="dynamically_created_div unique_identifier"></div>
<div class="dynamically_created_div unique_identifier"></div>
<div class="dynamically_created_div unique_identifier"></div>
</div>
</div>',
})
export class AppComponent{
hostEl: any;
constructor(
private el:ElementRef,
private renderer:Renderer2,
) {
this.hostEl = el.nativeElement;
}
runR(){
let change_this;
change_this= this.renderer.createElement('span');
this.renderer.addClass(change_this, 'change_this');
this.renderer.appendChild(this.hostEl, change_this);
}
}
Run Code Online (Sandbox Code Playgroud)
Angular2中有没有办法将HTML添加到.dynamically_created_div?因为上面只添加了组件的HTML结尾.
我也尝试过:
import { Component, ElementRef, ViewChild, Renderer, AfterViewInit } from '@angular/core';
@Component({
selector: 'my-app',
template: `<button (click)="runR()">Run</button>
<div …Run Code Online (Sandbox Code Playgroud) 我正在使用 angular 6 创建带有 promise 的 post 请求。在我的服务中,我创建了一个请求函数,如下所示:
sendRequest() {
let promise = new Promise((resolve, reject)=>{
this.http.post(this.url, this.params, {responseType: 'text'}).toPromise()
.then(res =>{
this.data = res;
this.router.navigateByUrl('1/success'); // I want to show res code on this page
resolve();
},
error => {
this.data = error;
this.router.navigateByUrl('1/fail');
reject(error);
}
)
return promise;
})}
Run Code Online (Sandbox Code Playgroud)
res 以 html 格式返回文本,如下所示:
<html>
<head>
some code
</head>
<body>
code i need
</body>
</html>
,这很好,但我想在成功/失败页面上使用该文本作为 html 代码。具体来说,body 标签中的所有内容。我怎样才能做到这一点?如果我使用类似的东西
<div [innerHTML]="res"></div>
Run Code Online (Sandbox Code Playgroud)
或者
{{res}}
Run Code Online (Sandbox Code Playgroud)
它只返回纯文本。