如何使角UI路由器的母公司状态始终执行控制器code状态的变化是什么时候? [英] How to make angular ui-router's parent state always execute controller code when state changes?

查看:102
本文介绍了如何使角UI路由器的母公司状态始终执行控制器code状态的变化是什么时候?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我们有我们的$ 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.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.

当用户提交表单时,我用的是$状态对象回到父页面:

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 状态到父本身,而不是我的资料<的孩子/ code>的状态,这个问题实际上消失,事情像预期(虽然,布局很糟糕像这样)。

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路由器也许是缓存父的结果,毫无防备的事情在模型中已经改变,它需要重新运行控制器?

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?

我怎能同时还具有父我的亲子关系我的资料页面始终执行其控制器重新加载其数据?基本上,我想只要使用 $状态或者点击一个链接,引导到我的资料这种情况发生。我该怎么做?

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 是默认的孩子状态我的资料 - 我怎么能做到这一点?

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路由器的母公司状态始终执行控制器code状态的变化是什么时候?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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