角度ui -路由器|多视图:如何在多个视图中使用解析对象?

时间:2022-02-04 10:31:27

I have a situation here, where inside one state, I have multiple views and each view has to fetch data from the server side, so I wanted to use 'resolve' in every view which makes its own REST call to get data from the server.

这里有一种情况,在一个状态中,我有多个视图,每个视图必须从服务器端获取数据,所以我想在每个视图中使用“resolve”,它自己调用REST来从服务器获取数据。

Following is my attempt :

以下是我的尝试:

.state('dashboard.xyz.deal.details', {
        url: "/details/:dealId",
        resolve: {
            permissions : function(dealDetails, $stateParams){
                return dealDetails.getUnitPermissions($stateParams.dealId);
            }
        },
        views: {

            "viewDealDetails": {
                templateProvider: function(permissions, $http){
                    return $http.get('/modules/deal-details-module/partial/views/view-deal-details/view-deal-details.html')
                        .then(function(tpl){
                        return tpl.data;
                    });
                },
                controller: 'ViewDealDetailsCtrl',
                resolve: {
                    resolveDealDetails : function(dealDetails, $stateParams){
                        console.log('Inside the resolve of viewDealDetails');
                        return dealDetails.getDealDetails($stateParams.dealId);
                    }
                }
            },
            "viewFinePrints": {
                templateProvider: function(permissions, $http){
                    return $http.get('/modules/deal-details-module/partial/views/view-fine-prints/view-fine-prints.html')
                        .then(function(tpl){
                            return tpl.data;
                        });
                },
                resolve: {
                    resolveFinePrints: function(dealDetails){
//How can I inject the 'resolveDealDetails' as dependency in 'resolveFinePrints' ?
                        console.log('Inside the resolve of resolveFinePrints ::::::');
                        return dealDetails.getFinePrints('travel').then(function(data){
                            return data;
                        });
                    }
                },
                controller: 'ViewFinePrintsCtrl'
            }
        }
    })

So, I wanted to ask following questions :

所以,我想问以下问题:

Q1. Is it correct to use 'resolve' inside the multiple views? As I have read it from the official documentation that

Q1。在多个视图中使用“resolve”是否正确?正如我从官方文件中看到的那样。

The resolve keyword MUST be relative to state not views (in case you use multiple views).

resolve关键字必须相对于state not views(如果您使用多个view)。

Q2. If resolving the dependencies in the views is OK, then how can I insert one resolved dependency inside another view ?

Q2。如果解析视图中的依赖项是OK的,那么如何在另一个视图中插入一个已解析的依赖项?

For eg. in my code I want use 'resolveDealDetails' as the dependency for 'resolveFinePrints'

如。在我的代码中,我希望使用'resolveDealDetails'作为'resolveFinePrints'的依赖项

2 个解决方案

#1


2  

Q2. If resolving the dependencies in the views is OK, then how can I >insert one resolved dependency inside another view ?

Q2。如果解析视图中的依赖项是OK的,那么>如何在另一个视图中插入一个已解析的依赖项?

Child/nested views inherit from parent view so data you resolve in parent is available in child. See doc

子/嵌套视图从父视图继承,因此在父视图中解析的数据在子视图中可用。看到医生

If you want to share data between controllers (in case if views are not nested) you should use service. Then inject it into controllers and thus needed data will be available where you want it. See related question

如果希望在控制器之间共享数据(如果视图不是嵌套的),应该使用service。然后将其注入控制器,因此需要的数据将在您需要的地方提供。看到相关的问题

#2


0  

So, following is the answer to the two questions posted :

下面是两个问题的答案:

Q1. Is it correct to use 'resolve' inside the multiple views? As I have read it from the official documentation that

Q1。在多个视图中使用“resolve”是否正确?正如我从官方文件中看到的那样。

The resolve keyword MUST be relative to state not views (in case you use multiple views).

resolve关键字必须相对于state not views(如果您使用多个view)。

As it turned out, the 'resolve' inside the nested views, worked just the way it works relative to state. So, the template of the nested views are rendered only after the dependencies are resolved.

事实证明,嵌套视图中的“解析”只是相对于状态的工作方式。因此,嵌套视图的模板只有在依赖项解析后才呈现。

Q2. If resolving the dependencies in the views is OK, then how can I insert one resolved dependency inside another view ?

Q2。如果解析视图中的依赖项是OK的,那么如何在另一个视图中插入一个已解析的依赖项?

In ui-router that resolved dependencies can't be provided as dependencies of the sibling view. We need to have a child-parent relationship among views to inject one resolved dependency into another view.

在ui-router中,解析的依赖项不能作为同级视图的依赖项提供。我们需要在视图之间建立子-父关系,以便将一个已解析的依赖项注入另一个视图。

I hope this will help others as well :)

我希望这对其他人也有帮助:

#1


2  

Q2. If resolving the dependencies in the views is OK, then how can I >insert one resolved dependency inside another view ?

Q2。如果解析视图中的依赖项是OK的,那么>如何在另一个视图中插入一个已解析的依赖项?

Child/nested views inherit from parent view so data you resolve in parent is available in child. See doc

子/嵌套视图从父视图继承,因此在父视图中解析的数据在子视图中可用。看到医生

If you want to share data between controllers (in case if views are not nested) you should use service. Then inject it into controllers and thus needed data will be available where you want it. See related question

如果希望在控制器之间共享数据(如果视图不是嵌套的),应该使用service。然后将其注入控制器,因此需要的数据将在您需要的地方提供。看到相关的问题

#2


0  

So, following is the answer to the two questions posted :

下面是两个问题的答案:

Q1. Is it correct to use 'resolve' inside the multiple views? As I have read it from the official documentation that

Q1。在多个视图中使用“resolve”是否正确?正如我从官方文件中看到的那样。

The resolve keyword MUST be relative to state not views (in case you use multiple views).

resolve关键字必须相对于state not views(如果您使用多个view)。

As it turned out, the 'resolve' inside the nested views, worked just the way it works relative to state. So, the template of the nested views are rendered only after the dependencies are resolved.

事实证明,嵌套视图中的“解析”只是相对于状态的工作方式。因此,嵌套视图的模板只有在依赖项解析后才呈现。

Q2. If resolving the dependencies in the views is OK, then how can I insert one resolved dependency inside another view ?

Q2。如果解析视图中的依赖项是OK的,那么如何在另一个视图中插入一个已解析的依赖项?

In ui-router that resolved dependencies can't be provided as dependencies of the sibling view. We need to have a child-parent relationship among views to inject one resolved dependency into another view.

在ui-router中,解析的依赖项不能作为同级视图的依赖项提供。我们需要在视图之间建立子-父关系,以便将一个已解析的依赖项注入另一个视图。

I hope this will help others as well :)

我希望这对其他人也有帮助: