承诺解决后如何加载html页面?

问题描述 投票:0回答:2

我正在尝试使用Angular和Typescript从ASP .NET Core API向html组件显示一些数据,响应返回正确的值,并且Promise正常工作。

问题是在解决诺言之前加载了html页面,因此显示的值是不确定的。

在解决诺言之后,我可以使用什么来使页面加载?

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;
}
angular typescript dependency-injection promise angular-resolver
2个回答
0
投票

由于FileShareComponent是触发服务调用的那个,除非创建了更为复杂的嵌套组件体系结构,否则您将无法阻止组件初始化,直到该调用完成为止。

我会隐藏这些值,直到Observable解析为止。而且,我认为没有紧迫的理由将可观察者转化为承诺。像这样的东西:

export class FileShareComponent implements OnInit {
  // create a variable to determine whether screen is initialized
  screenInitialized = false;
  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)
      // subscribe to Observable instead of converting it to a promise 
      .subscribe(res => {
        this.service.sharedFormData = res as ShareModel;
        // toggle initialized property
        this.initialized = true;
        console.log(this.service.sharedFormData);
      });
  }
  ngOnInit() {
  }
}

现在,只需将显示模板包装在div中,该div将一直隐藏直到加载数据:

<div *ngIf="initialized">
    <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>
</div>

我认为这是根据当前结构开始工作的最简单方法。如果我是从头开始构建它,那么我会考虑使用router resolver


0
投票

或更简单地说,您可以在HTML模板中使用Elvis运算符?.

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>
© www.soinside.com 2019 - 2024. All rights reserved.