当状态更改时,如何使有角度的ui路由器的父状态始终执行控制器代码? [英] How to make angular ui-router's parent state always execute controller code when state changes?

查看:118
本文介绍了当状态更改时,如何使有角度的ui路由器的父状态始终执行控制器代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们在$ stateProvider中定义了一个父子关系,如下所示:

Let's say we have a parent-child relationship defined in our $stateProvider as follows:

    .state('myProfile', {
        url: "/my/profile",
        templateUrl: 'my/profile.html',
        controller: 'MyProfileController'
    }).state('myProfile.edit', {
        url: "/edit",
        templateUrl: 'my/profile.edit.html',
        controller: 'EditMyProfileController'
    });

这里的想法是父 myProfile 状态是不可编辑的,而子 myProfile.edit 状态是编辑配置文件的实际形式.让我们忽略一下,这是否是个人资料页面的工作方式-我只是在玩弄东西和学习.

The idea here is that the parent myProfile state is non-editable, but the child myProfile.edit state is the actual form to edit the profile. Let's ignore if this is how a profile page should work - I'm just playing around with things and learning.

当用户提交表单时,我使用$ state对象返回到父页面:

When a user submits the form, I use the $state object to go back to the parent page:

$scope.save = function() {
    userResource.update($scope.user, function() {
        SessionService.refresh();

        $state.go('myProfile'); // also tried with reload: true as well
    });
}

在用户保存配置文件数据后-并且 被正确保存-除非我按F5键并刷新浏览器,否则父视图不会更新.

After the user saves the profile data - and it is getting saved properly - the parent's view does not get updated unless I hit F5 and refresh the browser.

我注意到的一件事是,如果我将子项 myProfile.edit 状态设置为父项本身而不是子项 myProfile 状态,则实际上这个问题就解决了离开,一切按预期进行(尽管布局看起来像这样).

One thing I have noticed is that if I make the child myProfile.edit state into a parent itself and not a child of the myProfile state, this problem actually goes away and things behave as expected (albeit, the layout looks bad like this).

似乎UI Router可能正在缓存父项的结果,是否怀疑模型中的情况已发生变化,是否需要重新运行控制器?

It seems that UI Router is maybe caching the result of the parent, unsuspecting that things have changed in the model and that it needs to rerun the controller?

如何保持父子关系,同时仍然使父 myProfile 页面始终执行其控制器以重新加载其数据?基本上,我希望每次使用 $ state 或单击指向 myProfile 的链接时都会发生这种情况.我该怎么办?

How can I keep my parent-child relationship while still having the parent myProfile page always execute its controller to reload its data? Basically, I want this to happen whenever $state is used or when a link is clicked that directs to myProfile. How can I do that?

如果我不能按照我的要求去做,那么我知道我可以设置一堆子状态并使它们按预期工作...但是,如何为父级设置默认的子状态?例如,假设我希望 myProfile.edit 成为 myProfile 的默认子状态-我该怎么做?

And if I cannot do what I ask, then I am aware that I can setup a bunch of child states and have them work as intended... however, how can I set the default child state for the parent? For example, let's say I want myProfile.edit to be the default child state of myProfile - how can I do that?

谢谢!

推荐答案

要在从子状态导航到父状态时强制重新加载父状态,可以使用以下两种方式.纯js或指令属性.

To force a parent state to reload when navigate to him from a child state you can use both ways below. Pure js or a directive attribute.

<a ui-sref="myProfile" ui-sref-opts="{ reload: true }">Back to My Profile</a>

$state.go('myProfile', null, { reload: true });

这篇关于当状态更改时,如何使有角度的ui路由器的父状态始终执行控制器代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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