Code Monkey home page Code Monkey logo

Comments (4)

haoliangwu avatar haoliangwu commented on June 11, 2024 1

hi @Albejr ,

I try to reproduce issue with the following code:

    this.service.showGroup('zone1')
    this.service.showGroup('zone2')

    setTimeout(() => {
      this.service.hideGroup('zone1')
    }, 1000)

    setTimeout(() => {
      this.service.hideGroup('zone2')
    }, 3000)

but everything works fine.

what do you mean about "the second '2' is not displayed" ? There are maybe two scenarios about it:

  • first one is the the response of the api '/api/pet' return particularly fast, so the spinner comp was detached immediately when it was attached to mask container dom element. so it seems "not displayed".
  • second one is when you invoke showGroup(), the second 2 group got the attach event from the service instance and try to attached spinner comp to the mask container dom element, but currently the container dom element maybe still not loaded(mounted into the document). in this case, the attach process will fail silently.

resolution

  • about the case 1, I think the case is expected result.
  • about the case 2, try to invoke showGroup() after the container dom element has loaded, for example, use ngAfterViewInit or other technology.

if you have any questions, fell free to let me know, thx.

from ngx-loading-mask.

haoliangwu avatar haoliangwu commented on June 11, 2024

FYI, the library use observer pattern to process the multiples directive communication. The loadingMaskService act as Observable(multi cast) and the multiple directives act as Observer, so I guess the issue occurred in spinner attaching process.

from ngx-loading-mask.

Albejr avatar Albejr commented on June 11, 2024

It really worked in the ngAfterViewInit event.
But, the GET services are in the ngOnInit event.

https://stackblitz.com/edit/haoliangwu-ngxloadingmask

from ngx-loading-mask.

haoliangwu avatar haoliangwu commented on June 11, 2024

hi @Albejr ,

if you have to put the business code into ngOnInit lifecycle, to wait the container element loaded, you have two options:

  • put the code into setTimeout

something like:

  // invoke the code into next event tick
  setTimeout(() => {
      this.loadingMaskService.showGroup('zone1');
      this.loadingMaskService.showGroup('zone2');

      setTimeout(() => {
        this.loadingMaskService.hideGroup('zone1');
      }, 5000);

      setTimeout(() => {
        this.loadingMaskService.hideGroup('zone2');
      }, 3000);
  })
  • inject ChangeDetectorRef and trigger change detection manually

the code is:

constructor(
    private loadingMaskService: LoadingMaskService,
    private cdr: ChangeDetectorRef
  ) { }

  ngOnInit(): void {
      // trigger change detection manually
      this.cdr.detectChanges()

      this.loadingMaskService.showGroup('zone1');
      this.loadingMaskService.showGroup('zone2');

      setTimeout(() => {
        this.loadingMaskService.hideGroup('zone1');
      }, 5000);

      setTimeout(() => {
        this.loadingMaskService.hideGroup('zone2');
      }, 3000);
  }

from ngx-loading-mask.

Related Issues (9)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.