2

Nativescript-angular after http service call, view is not updated (Angular 5 ).

Find the below code where I have written the http service call then the response is assigned to view which is not updating in view page. If I do any other event like mouse over text box, the view is getting updated. componet.ts file

this.orderService.getProductBySKU('DR1056.01534XXL')
    .subscribe(resProduct => {
        this.ngZone.run(() => {
            this.processing = false;                
         });
    }

Service file

getProductBySKU(sku) {
    const authToken = localStorage.getItem('currentUserToken');
    return this.http.get(this.config.apiUrl + 'get-product-by-sku?sku=' + sku, this.config.basicAuth())
 .map(response => response);

}

Note : By default this.processing is given as True in constructor which displays in view, after service call this.processing is assigned as False, but which displays only True. If I click any text box or button click, this.processing is changed to False in view.

1 Answer 1

3

First off I'd recomend using the HttpClient params option to avoid potential URL escaping problems:

this.http.get(this.config.apiUrl + 'get-product-by-sku', {...this.config.basicAuth(), params: { sku: sku }})

```

As for the processing, try forcing a change detection:

constructor(private ref: ChangeDetectorRef) {}
(...)
this.orderService.getProductBySKU('DR1056.01534XXL')
    .subscribe(resProduct => {
        this.ngZone.run(() => {
            this.processing = false;                
            this.ref.detectChanges();
         });
    }

If that doesn't work, please provide a snippet of your template.

Sign up to request clarification or add additional context in comments.

3 Comments

It does work. But why we should force to update the view. In earlier, I didn't do force even it works.
Yeah, it should work. I really can't tell why it isn't. I had to use ngZone a lot of times in Nativescript, but I'm not sure why it isn't working for you.
I am also facing the same issue? Anybody know specific reason why it's not working without firing change detection?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.