gpt4 book ai didi

AngularJS UI 路由器 $state 仅重新加载子状态

转载 作者:行者123 更新时间:2023-12-03 09:17:55 24 4
gpt4 key购买 nike

我将 UI 路由器用于主菜单的选项卡以及状态之一(用户)内的链接。用户状态有一个用户列表,当用户被点击时,我只想重新加载子状态,但它正在重新加载子状态和父状态(用户)。

这是我的代码:

    app.config(function ($stateProvider, $locationProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise("/");
$locationProvider.html5Mode(true);
$stateProvider
.state('home', {
abstract: true,
template: '<div ui-view=""></div>',
controller: 'HomeController as homeCtrl'
})
.state('users', {
parent: 'home',
url: '/users',
templateUrl: '/User/Index',
controller: 'UserController as userCtrl',
})
.state('users.createUser', {
templateUrl: '/User/CreateUser',
controller: 'CreateUserController as cuCtrl'
})
.state('users.editUser', {
templateUrl: '/User/EditUser',
controller: 'EditUserController as euCtrl',
resolve: {
userInfo: function (contextInfo, userData) {
return userData.get(contextInfo.getUserKey());
}
}
})
.state('users.addWebItemsForUser', {
templateUrl: '/User/AddWebItemsForUser',
controller: 'UserWebItemsController as uwiCtrl'
})
.state('users.addReportsForUser', {
templateUrl: '/User/AddReportsForUser',
controller: 'UserReportsController as urCtrl'
})
.state('users.userGroups', {
templateUrl: '/User/UserGroups',
controller: 'UserGroupController as userGroupCtrl'
})
//.state('users.logInWebApp', {
// template: '<h3>Logging into web app</h3>',
// resolve: {
// user: function() {
// return {
// //companyId: contextInfo.getCustomerKey()
// //userId:
// //password:
// }
// }
// },
// controller: function() {
// // need company code, username and password of user then need to open window with webapp url (will that work?) - do we still want to add this functionality?
// }
//})
.state('links', {
url: '/links',
templateUrl: '/Link/Index',
controller: 'LinkController as linkCtrl'
})
.state('onlrpts', {
url: '/onlineReports',
templateUrl: '/OnlineReport/Index',
controller: 'OnlineReportController as onlRptCtrl'
})
.state('reports', {
url: '/customerReports',
templateUrl: '/CustomerReport/Index',
controller: 'CustomerReportController as custRptCtrl'
});
})
.config(function($provide) {
$provide.decorator('$state', function($delegate, $stateParams) {
$delegate.forceReload = function() {
return $delegate.go($delegate.$current.name, $stateParams, {
reload: true,
inherit: false,
notify: true
});
};
return $delegate;
});
});

这是单击用户时调用的父 Controller (UserController) 中的函数:
            this.userTreeClick = function(e) {
contextInfo.setUserKey(e.Key);
contextInfo.setUserName(e.Value);
userCtrl.userKey = e.Key;
$state.forceReload();
};

所以说我处于 users.userGroups 状态,当我点击另一个用户时,我只想重新加载 users.userGroups 状态。这可能吗?我已经通过谷歌和 StackOverflow 寻找答案,但我没有找到任何与我想要做的完全一样的东西。当我中断检查 $state.$current.name 时 - 名称是正确的 - users.userGroups,但它重新加载所有内容而不仅仅是子状态。很感谢任何形式的帮助!

最佳答案

如 API 引用中所述,我们可以使用 $state.reload :

$state.reload(state)

A method that force reloads the current state. All resolves are re-resolved, controllers reinstantiated, and events re-fired.

Parameters:

  • state (optional)
    • A state name or a state object, which is the root of the resolves to be re-resolved.


一个例子:
//will reload 'contact.detail' and 'contact.detail.item' states
$state.reload('contact.detail');

类似的我们可以用 $state.go() 来实现和它的 options范围:

$state.go(to, params, options)

...

  • options (optional)
    • location ...
    • inherit ...
    • relative ...
    • notify ...
    • reload (v0.2.5) - {boolean=false|string|object}, If true will force transition even if no state or params have changed. It will reload the resolves and views of the current state and parent states. If reload is a string (or state object), the state object is fetched (by name, or object reference); and \ the transition reloads the resolves and views for that matched state, and all its children states.


来自 https://github.com/angular-ui/ui-router/issues/1612#issuecomment-77757224 的示例克里斯·T:
{ reload: true } // reloads all,
{ reload: 'foo.bar' } // reloads top.bar, and any children
{ reload: stateObj } // reloads state found in stateObj, and any children

一个例子
$state.go('contact', {...}, {reload: 'contact.detail'});

关于AngularJS UI 路由器 $state 仅重新加载子状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25316591/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com