为什么我必须调用NgZone.run来查看在Angular2中使用breezejs进行更新? [英] Why do i have to call NgZone.run for my view to update in Angular2 with breezejs?

查看:101
本文介绍了为什么我必须调用NgZone.run来查看在Angular2中使用breezejs进行更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习angular2,并使用odata webapi后端创建了一个测试应用程序。
在应用程序中,我有一个视图可以获取一系列的项目,我想在我的视图中显示这些。

I am trying to learn angular2 and created a test application with an odata webapi backend. In the application i have a view which fetches an array of items and i want to show these in my view.

对于从前端获取数据我是使用breezejs库,因为它已经证明在过去节省了很多时间,我喜欢用odata后端使用它。

For fetching data from the frontend i am using breezejs library since it has proved to save me alot of time in the past and i like using it with an odata backend.

调用树和应用程序结构如下所示:

The call tree and application structure looks like this:

通过从视图中调用服务功能开始获取项目,开始调用(请注意,我正在从每个通话中返回一个es-6承诺):

Call starts by calling a service function from the view to start fetching the items (note that i am returning an es-6 promise from every call):

this._scrumModuleService.fetchActiveSessions().then((sessions: ScrumSession[]) => {
    // Here i have to call zone.run else my view wont update.
    this._zone.run(() => {
        this.sessions = sessions;
    });
}).catch((error: any) => {
     debugger;
});

然后从视图中,它将转到那个又调用存储库的服务:

Then from the view it will go to the service which in turn calls the repository:

public fetchActiveSessions(): Promise<ScrumSession[]> {
    return this._scrumSessionRepository.fetchActiveSessions();
}

存储库提取功能:

The repository fetch function:

public fetchActiveSessions(): Promise<ScrumSession[]> {
    return this._dataContext.fetch(new breeze.EntityQuery().from("ScrumSessions").expand(['creator', 'scrumRoom','productOwner', 'users']));
}

然后最终存储库调用(通用)数据文本,将执行查询微风实体管理员:

Then eventually the repository calls the (generic) datacontext which will execute the query with the breeze entitymanager:

public fetch(query: breeze.EntityQuery, isRetry: boolean = false): Promise<any> {

    return new Promise((resolve, reject) => {
            this.entityManager.executeQuery(query).then((result: breeze.QueryResult): void => {
            // Data has been fetched, resolve the results
            resolve(result.results);
        });
    });
}

现在,您可以在视图中看到我必须使用运行功能NgZone或者我的观点不会更新。我想知道为什么我必须这样做,因为我期待角度2自动看到这个。
我已经挖掘了几个similair问题,找不到答案。我还包括另一个线程中建议的angular2-polyfills脚本,但是没有解决它。

Now as you can see in the view i have to use the run function from NgZone or else my view wont update. I am wondering why i have to do this since i was expecting angular2 to see this for me automatically. I have digged through hseveral similair questions and could not really find an answer yet. I have also included the angular2-polyfills script as suggested in another thread but that did not solve it.

我缺少什么或为了我的观点执行什么自动更新而不调用zone.run?

What am i missing or what do i have to implement for my view to update automatically without calling zone.run?

推荐答案

角色运行在大多数异步API被修补的区域中。当异步调用完成时角度运行更改检测。

Angular runs in a zone where most async APIs are patched. When an async call is completed Angular runs change detection.

不知何故微风代码离开角度区域并断开更改检测。这是因为您从外部初始化微风Angular或微风使用一些不受Angulars区域修补的异步API,因此在Angulars区域之外执行回调。

Somehow breeze code leaves Angulars zone and "breaks" change detection. This is either because you initialize breeze from outside Angular or breeze uses some async API that is not patched by Angulars zone and therefore callbacks are executed outside Angulars zone.

这篇关于为什么我必须调用NgZone.run来查看在Angular2中使用breezejs进行更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆