ngOnInit hasn't finished fetching data when ngAfterViewInit was ran

Discussão em 'Angular' iniciado por user3714598, Fevereiro 8, 2025.

  1. user3714598

    user3714598 Guest

    I have a code that looks like this. I expect that it will finish running fetchData function before calling ngAfterViewInit but its not. Do you have an idea what to check? Thank you!

    @Component({
    selector: 'my-cmp',
    templateUrl: './my-cmp.component.html',
    changeDetection: ChangeDetectionStrategy.OnPush
    })

    export class MyCmpComponent implements OnInit, AfterViewInit, OnDestroy {

    public var1;
    public var2;
    ..

    constructor() {}

    ngOnInit() {
    this.var1 = 'blaa';
    this.fetchData(x);
    }

    ngAfterViewInit {
    // do this
    }

    private fetchData(x: string) {
    // http request
    this.fetchIt(x).subscribe(response => {
    //initialize
    });
    }

    Continue reading...

Compartilhe esta Página