New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
UI not updating when retrieving data via fetch #1913
Comments
Could you give more info info on where |
@mjw56 Could you please also try: Zone.bindPromiseFn(fetch(...)).then(...); and reports if helps ? |
@vicb I'm calling fetch from a Util API which I inject into @Component({
selector:'app',
injectables: [API]
}) and the import {NgZone} from 'angular2/src/core/zone/ng_zone';
declare var fetch;
export class API {
getBooks() {
NgZone.bindPromiseFn(fetch('http://localhost:8080/books')).then((r) => {
return r;
});
}
} I had to add Zones to declare module "angular2/src/core/zone/ng_zone" {
export var NgZone: {
bindPromiseFn: Function
};
} but when I run it I get a new error: TypeError: ng_zone_1.NgZone.bindPromiseFn is not a function
TypeError: ng_zone_1.NgZone.bindPromiseFn is not a function
at API.getBooks (api.js:6)
at new App (app.ng2.js:22)
at execute.ReflectionCapabilities.factory (angular2.dev.js:7878)
at ElementInjector.execute.ElementInjector._new (angular2.dev.js:24343)
at ElementInjector.execute.ElementInjector._getDirectiveByKeyId (angular2.dev.js:24623)
at ElementInjector.execute.ElementInjector.instantiateDirectives (angular2.dev.js:24209)
at AppViewManagerUtils.execute.AppViewManagerUtils._hydrateView (angular2.dev.js:11609)
at AppViewManagerUtils.execute.AppViewManagerUtils.attachAndHydrateInPlaceHostView (angular2.dev.js:11533)
at AppViewManager.execute.AppViewManager.createInPlaceHostView (angular2.dev.js:23221)
at angular2.dev.js:13826 I gotta run but will be back later to give this another try. Thanks! |
Sorry I've seen the gist afterwards.
|
Ah, my bad. I can definitely play around with this more later. Does something like this look like the right approach? declare var fetch, Zone;
export class API {
getBooks() {
Zone.bindPromiseFn(fetch('http://localhost:8080/books')).then(r => r);
}
} but I get an error TypeError: Zone.bindPromiseFn(...).then is not a function |
Hum, my bad. Could you please try: getBooks() {
Zone.bindPromiseFn(() => fetch('http://localhost:8080/books')).then(r => r);
} Edit: actually, you want to return the value: getBooks() {
return Zone.bindPromiseFn(() => fetch('http://localhost:8080/books'));
} |
Actually:
Should be the last version :) |
That works! w00t! Thank you @vicb! :) |
What is your browser ? |
Oh yes, I am only testing this on Chrome Version 44.0.2398.0 dev (64-bit) |
Thanks ! |
It seems that using (some?) Promise polyfills might prevent bindPromiseFn from fixing this issue. I was having the same issue with fetch and, after applying bindPromiseFn to my fetch call, I still had to remove the polyfill that the fetch authors recommend (https://github.com/jakearchibald/es6-promise) before I could get the UI to update when I got my data out the response. Edit: Moving the polyfill script so it loads before angular also remedies the issue, so it just looks like the polyfill is overwriting something that it probably shouldn't. |
@jon-hall There is no need to use any Promise polyfill with Angular because it comes with a Promise monkey patch (based on es6-promise) whether native Promises are available or not. The next release of zone.js (which should happen later this week) will integrate the patching of the |
@mjw56 zone has been updated to 0.5.1 in master. The workaround is no more required starting from next alpha (.27) |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
I'm trying to use fetch to retrieve data, but the UI is not updating when I bind the data retrieved. Is there a way I can do this, or should I only be using the XHR module?
https://gist.github.com/mjw56/1c44e0aba514f1952235
The text was updated successfully, but these errors were encountered: