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

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

问题描述

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

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

我的ui-view"内容如下:

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

myApp.config(function ($stateProvider, $urlRouterProvider){$stateProvider.state("state1", {网址:#",模板:<p>状态1</p>",控制器:Ctrl1"}).state("state2", {网址:#",模板:<p>状态2</p>",控制器:Ctrl2"});});

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

这是这个问题的 JSFiddle.

解决方案

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

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

在此处找到此提示:

https://github.com/angular-ui/ui-router/commit/91a568454cc6300c54cc6300c5a8c5c6300c5a80c9be1130b1c5一个>

希望能帮到你

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>

My "ui-view" content is as follows:

<div ui-view></div>

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?

Here is the JSFiddle of this question.

解决方案

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>

Found this tip here:

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

Hope it could help

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

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