Html Angular5:微调器在页面加载时无法正常工作

Html Angular5:微调器在页面加载时无法正常工作,html,angular,typescript,service,components,Html,Angular,Typescript,Service,Components,下面是我的服务,其中我发出两个事件dataLoaded和loadeingdata,我想分别在加载和加载数据时显示和隐藏GIF图像,在组件中我捕获这两个事件并更改布尔变量的值。但是当第一次加载页面时,最初加载的GIF不显示,当加载的数据显示GIF时,如果我选择filter进行搜索,则它工作正常,并且当我调试代码时,showLoading的值成功更新 服务 import { forwardRef, Injectable, EventEmitter } from '@angular/core';

下面是我的服务,其中我发出两个事件
dataLoaded
loadeingdata
,我想分别在加载和加载数据时显示和隐藏GIF图像,在组件中我捕获这两个事件并更改布尔变量的值。但是当第一次加载页面时,最初加载的GIF不显示,当加载的数据显示GIF时,如果我选择filter进行搜索,则它工作正常,并且当我调试代码时,
showLoading
的值成功更新

服务

import { forwardRef, Injectable, EventEmitter } from '@angular/core';
    import { Http, Headers, RequestOptions, Response, URLSearchParams } from '@angular/http';
    import 'rxjs/add/operator/toPromise';


    @Injectable()
    export class SearchService {
     loadingData = new EventEmitter();
        dataLoaded = new EventEmitter();
     pages: any = {};
        searchResults = [];
     search() {
            this.loadingData.emit("ShowLoading")
            this.performSearch().then(response => {
                this.searchResults = response.json();
                this.pages = this.searchResults['pages'];
                this.searchResults = this.searchResults['data'];
                this.resultsUpdated.emit('SearchResultsUpdated');
                this.dataLoaded.emit("HideLoading")
            }, error => {
                   this.dataLoaded.emit("HideLoading")
             });
        }
    }
组件

import { Component, ViewChild, ElementRef } from '@angular/core';
import { SearchService } from '../../search.service';
/*import {Alert} from "../../alert.component";*/
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
import { SavedSearchComponent } from '../saved-search/saved-search.component';
@Component({
 selector: 'property-onion-search-results',
  templateUrl: './search-results.component.html'
})

export class SearchResultsComponent {
  @ViewChild('searchResultArea') resultsArea: ElementRef;
  searchResults: any[] = null;
  closeResult: string;
  view = 'regular';
  resultInformation: any[];
  filterString: string = "";
  resultCount: number = 0;
  numbers: any[] = null;
  showLoading;

  constructor(private searchService: SearchService, private modalService: NgbModal) {

    this.searchService.resultsUpdated.subscribe(data => {
      this.searchResults = this.searchService.getResults();
      this.filterString = this.searchService.toString();
      this.resultCount = this.searchService.pages.total;
      var pages = this.searchService.pages.number_of_pages;
      if (this.searchService.pages.total > 25) {
        this.numbers = new Array(pages).fill(1).map((x, i) => i);
      } else {
        this.numbers = null;
      }
      this.resultsArea.nativeElement.click(); // THis is a hack to fix refresh issue of this area. This area doesn't get refreshed unless you click somewhere in the browser.          
    });

    this.searchService.loadingData.subscribe(data => {
      console.log("show")
      this.showLoading = true
    });
    this.searchService.dataLoaded.subscribe(data => {
      console.log("hide")
      this.showLoading = false
    });

  }
}
html

<img class="loading" *ngIf="showLoading" src="images/loader.gif"/>

对于初始加载,您应该直接在组件中定义显示加载:

showLoading:boolean = true;