“ui-sref"单击两次链接时,不会刷新“ui-view"(其控制器不会重新运行) [英] "ui-sref" does not refresh the "ui-view"(its controller does not rerun) when a link is clicked twice

查看:21
本文介绍了“ui-sref"单击两次链接时,不会刷新“ui-view"(其控制器不会重新运行)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当第一次点击一个状态时,拥有ui-view"属性的div被替换为该状态的模板".但是,当我再次单击相同的状态时,它的控制器不会重新加载.我怎样才能让这个控制器再次加载?

When a state is clicked at first, the div that owns the "ui-view" attribute is replaced with the "template" of that state. However, when I click the same state again, its controller does not reloaded. How can I make this controller to load again?

例如,假设我有以下导航菜单:

For example, suppose I have the following navigation menu:

<nav> 
    <a ui-sref="state1">State 1</a>
    <a ui-sref="state2">State 2</a>
</nav>

我的ui-view"内容如下:

My "ui-view" content is as follows:

<div ui-view></div>

我的状态描述如下.当我点击导航菜单上的State1"时,我希望在开发者控制台上观察到文本Ctrl 1 loaded".如果是第一次,那么我会观察该文本.但是,当我重新单击相同的链接时,控制器没有再次加载,即我看不到控制台上显示的文本 "Ctrl 1 loaded" 两次.

My states are described as below. When I click the "State1" on the navigation menu, I expect to observe the text "Ctrl 1 loaded" on the developer console. If it's first time, then I observe that text. However, when I re-click the same link, the controller is not loaded again that is I couldn't see the text "Ctrl 1 loaded" displayed on the console twice.

myApp.config(function ($stateProvider, $urlRouterProvider){
    $stateProvider.state("state1", {
        url: "#",
        template: "<p>State 1</p>",
        controller: "Ctrl1"
      }).state("state2", {
        url: "#",
        template: "<p>State 2</p>",
        controller: "Ctrl2"
      });
});

为什么?你有什么想法吗?

Why? Do you have any idea?

这是这个问题的 JSFiddle.

推荐答案

就像 Philipp 说的,这是因为这是 ui-router 的默认行为,但是如果您想在每次单击链接时重新加载控制器,你其实可以使用 ui-sref-opts 属性来强制它,像这样:

Like Philipp said, it is because this is the default behavior of ui-router, BUT if you would like to reload your controller each time you click on your link, you can actually use the ui-sref-opts attribute to force it, like this:

<a ui-sref="state1" ui-sref-opts="{reload: true}">State 1</a>

在这里找到这个提示:

https://github.com/angular-ui/ui-router/commit/91a568454cc600aa4f34dedc8a80c9be1130b1c5

希望对你有帮助

这篇关于“ui-sref"单击两次链接时,不会刷新“ui-view"(其控制器不会重新运行)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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