ege*_*ari 28 javascript state angularjs angular-ui angular-ui-router
假设我们在$ stateProvider中定义了一个父子关系,如下所示:
.state('myProfile', {
url: "/my/profile",
templateUrl: 'my/profile.html',
controller: 'MyProfileController'
}).state('myProfile.edit', {
url: "/edit",
templateUrl: 'my/profile.edit.html',
controller: 'EditMyProfileController'
});
Run Code Online (Sandbox Code Playgroud)
这里的想法是父myProfile状态是不可编辑的,但子myProfile.edit状态是编辑配置文件的实际形式.让我们忽略,如果这是个人资料页面的工作方式 - 我只是在玩弄东西和学习.
当用户提交表单时,我使用$ state对象返回父页面:
$scope.save = function() {
userResource.update($scope.user, function() {
SessionService.refresh();
$state.go('myProfile'); // also tried with reload: true as well
});
}
Run Code Online (Sandbox Code Playgroud)
之后,用户保存配置数据-这是得到正确保存-父母的观点并没有得到更新,除非我打了F5并刷新浏览器.
我注意到的一件事是,如果我将子myProfile.edit状态变为父本身而不是状态的子节点myProfile,这个问题实际上消失了,事情就像预期的那样(虽然布局看起来很糟糕).
似乎UI路由器可能会缓存父级的结果,不知道模型中的内容已经发生变化,是否需要重新运行控制器?
如何在父myProfile页面始终执行其控制器以重新加载其数据的同时保持父子关系?基本上,我希望无论何时$state使用或点击链接指向时都会发生这种情况myProfile.我怎样才能做到这一点?
如果我不能按照我的要求做,那么我知道我可以设置一堆子状态并让它们按预期工作......但是,如何为父设置默认子状态?例如,假设我想myProfile.edit成为默认的子状态myProfile- 我该怎么做?
谢谢!
Afo*_*aça 35
要在从子状态导航到父级时强制父状态重新加载,您可以使用以下两种方式.纯js或指令属性.
<a ui-sref="myProfile" ui-sref-opts="{ reload: true }">Back to My Profile</a>
Run Code Online (Sandbox Code Playgroud)
要么
$state.go('myProfile', null, { reload: true });
Run Code Online (Sandbox Code Playgroud)