具有解析的Angularjs ui-router抽象状态

时间:2022-02-15 13:02:42

Is it possible to make an abstract state with ui-router that needs to resolve data? I need to load the top half of a page with profile info, and then have a sub-nav that loads other details at the bottom of the page. The idea is that the abstract state would load the profile info every time regardless of which child state I am on. It doesn't seem to work.

是否可以用需要解析数据的ui-router创建抽象状态?我需要在页面的上半部分加载概要信息,然后在页面底部有一个子导航来加载其他细节。其思想是,无论我处于哪个子状态,抽象状态都会每次加载概要信息。这似乎行不通。

Example:

例子:

.state('app.profile', {
    abstract: true,
    url: 'user/:username',
    views: {
        content: {
            templateUrl: 'views/frontend/profile.html',
            controller: 'ProfileCtrl',
            resolve: {
                data: ['$stateParams', 'ProfileService', function ($stateParams, ProfileService) {
                    var username = $stateParams.username;
                    return ProfileService.getProfile(username);
                }]
            }
        }
    }
})
.state('app.profile.something', {
    url: '',
    views: {
        profile: {
            templateUrl: 'views/frontend/profile.something.html',
            controller: 'ProfileCtrl',
            resolve: {
                data: ['$stateParams', 'ProfileService', function ($stateParams, ProfileService) {
                    var username = $stateParams.username;
                    return ProfileService.getProfileSomething(username);
                }]
            }
        }
    }
})

1 个解决方案

#1


11  

Solution here is to distinguish the names of the resolved data. Check this answer:

这里的解决方案是区分已解析数据的名称。检查这个答案:

And its plunker

和它的一美元

So in our case we would need this change in parent

在我们的例子中我们需要父结点的这个变化

resolve: {
    dataParent: ['$stateParams', 'ProfileService', function ($stateParams, ProfileService) {
        var username = $stateParams.username;
        return ProfileService.getProfile(username);
    }]
}

and this in child

和这个孩子

resolve: {
    dataChild: ['$stateParams', 'ProfileService', function ($stateParams, ProfileService) {
        var username = $stateParams.username;
        return ProfileService.getProfileSomething(username);
    }]
}

so, instead of working with resolve : { data: ... } in parent and child we would have these:

因此,与其使用resolve: {data:…在父母和孩子中,我们有:

// parent state
resolve : { dataParent: ... } 
// child state
resolve : { dataChild: ... } 

One working example should be better than other words...

一个工作的例子应该比其他的词更好……

#1


11  

Solution here is to distinguish the names of the resolved data. Check this answer:

这里的解决方案是区分已解析数据的名称。检查这个答案:

And its plunker

和它的一美元

So in our case we would need this change in parent

在我们的例子中我们需要父结点的这个变化

resolve: {
    dataParent: ['$stateParams', 'ProfileService', function ($stateParams, ProfileService) {
        var username = $stateParams.username;
        return ProfileService.getProfile(username);
    }]
}

and this in child

和这个孩子

resolve: {
    dataChild: ['$stateParams', 'ProfileService', function ($stateParams, ProfileService) {
        var username = $stateParams.username;
        return ProfileService.getProfileSomething(username);
    }]
}

so, instead of working with resolve : { data: ... } in parent and child we would have these:

因此,与其使用resolve: {data:…在父母和孩子中,我们有:

// parent state
resolve : { dataParent: ... } 
// child state
resolve : { dataChild: ... } 

One working example should be better than other words...

一个工作的例子应该比其他的词更好……