【发布时间】:2020-01-05 21:05:07
【问题描述】:
我正在尝试使用 Angular 和 Typescript 从 ASP .NET Core API 向 html 组件显示一些数据,响应返回正确的值并且承诺运行良好。
问题是 html 页面在 promise 解决之前加载,所以显示的值是未定义的。
promise 解决后我可以使用什么来使页面加载以及如何加载?
fileshare.component.ts
export class FileShareComponent implements OnInit {
constructor(
private route: ActivatedRoute,
private router: Router,
private service: UserService,
private http: HttpClient
) {
const file: string = this.route.snapshot.queryParamMap.get('file');
this.http.get(this.service.BaseURL + '/Share?IdentityString=' + file)
.toPromise()
.then(res => {
this.service.sharedFormData = res as ShareModel;
console.log(this.service.sharedFormData);
});
}
ngOnInit() {
}
}
fileshare.component.html
<pre><code class="hljs">Name:
{{service.sharedFormData.Name}}</code></pre>
<pre><code class="hljs">Description:
{{service.sharedFormData.Description}}</code></pre>
<pre><code class="hljs">Syntax:
{{service.sharedFormData.Syntax}}</code></pre>
<pre><code class="hljs">LastModified:
{{service.sharedFormData.LastModified | date: "HH:mm dd/MM/yyyy"}}</code></pre>
<pre><code class="hljs">Content:
{{service.sharedFormData.Content}}</code></pre>
<div class="form-group text-center mt-4">
<button class="btn btn-dark" (click)="this.service.raw()" type="submit"><i class="fas fa-database"></i>
Raw</button>
</div>
share-model.model.ts
export class ShareModel {
Id:number;
Name: string;
Description: string;
Syntax: string;
IdentityString:string;
LastModified: string;
Content: string;
FileId: number;
}
【问题讨论】:
-
您是否在
console.log(this.service.sharedFormData);中获取值? -
尝试将
this.changeDetectorRef.detectChanges()放在then()块内this.service.sharedFormData = res as ShareModel;之后,但这不是必需的。也试试看,让我知道 -
console.log(this.service.sharedFormData)中有值;但是这些值在 html 请求后显示在控制台中。现在我来看看路由器解析器。
标签: angular typescript dependency-injection promise angular-resolver