简体   繁体   English

角度ui路由器仅刷新页面的一部分,需要多个视图吗?

[英]angular ui-router refresh only one part of the page, need multiple views?

I'm building an angular app where I present a navigation bar on the left with normal ui-sref's going to states like schedules and clients . 我正在构建一个有角度的应用程序,在其中我在左侧显示一个导航栏,其中普通的ui-sref会进入schedulesclients等状态。 On the clients view I present a list of clients (which animates in, slides in from left). 在客户端视图上,我显示了一个客户端列表(动画,从左向内滑动)。 Then I want to achieve the follow: 然后我要实现以下目标:

  1. When a client is clicked on, load their information into the main part of the screen 单击客户端时,将其信息加载到屏幕的主要部分
  2. Update the URL so that if they were to refresh at this point the same client would be selected 更新URL,以便如果此时刷新它们,则将选择同一客户端
  3. DO NOT re-instantiate the clients controller, as doing so re-animates the client list sliding in. 不要重新实例化客户端控制器,因为这样做会重新动画化滑入的客户端列表。

I've got #1 working, but can't get 2 or 3 to work in a way that I want. 我有#1工作,但无法以我想要的方式让2或3工作。 I can get the url to update, but doing so re-instantiates the clients controller and re-animates the client list, and no amount of {notify: false} or any other combo of options I've tried seems to do the trick. 我可以获取要更新的url,但是这样做可以重新实例化客户端控制器,并为客户端列表重新设置动画,并且我尝试过的{notify: false}或任何其他选项组合似乎都无法解决问题。 I did see the $urlRouter.deferIntercept() but I'm not sure how that applies to this situation. 我确实看到了$urlRouter.deferIntercept()但不确定如何将其应用于这种情况。 Do I need multiple views to achieve this, where clicking on a client just updates the "profile" section of the page? 我是否需要多个视图来实现这一点,在其中单击客户端只会更新页面的“配置文件”部分? Thanks so much! 非常感谢!

You can achieve this by loading a particular client in a substate of clients , with an associated URL that identifies the client. 您可以通过在一个子状态加载特定客户实现这一目标clients ,与识别客户端相关的URL。

$stateProvider.state('clients', {
    url: 'clients/',
    resolve: {
        clients: function () {
            // return the clients collection or a promise which will resolve with it
        }
    }
    // template, controller, etc
});

$stateProvider.state('clients.client', {
    url: 'client/{id:[1-9][0-9]+}/', // as this is a substate, this gets appended the the parent state's URL for an end result of something like /#/clients/client/1
    resolve: {
        client: ['$stateParams', 'clients', function ($stateParams, clients) {
            // Lookup the client in clients using $stateParams.id and return it or a promise that will resolve with it
        }]
    }
    // template, controller, etc
});

Then put a ui-view in the template for the clients state to give the clients.client substate somewhere to render. 然后在clients状态的模板中放置一个ui-view ,以将clients.client子状态提供到某个要呈现的位置。

When you first load any URL that starts with clients/ you'll enter the clients state. 首次加载任何以clients/开头的URL时,您将进入clients状态。 Navigation to and between substates will only run the substate transition code (resolves, controller, etc). 导航到子状态或在子状态之间导航只会运行子状态转换代码(解析,控制器等)。 Until you leave the clients parent state and return, the clients transition code will not run again. 在离开clients父状态并返回之前, clients转换代码将不会再次运行。

The ui.router readme has good info about nesting states, and they a provide demo app that really helped me understand the idea of a state hierarchy. ui.router自述文件具有有关嵌套状态的良好信息,并且它们提供了演示应用程序 ,确实帮助我了解了状态层次结构的概念。 That demo bears a lot of similarity to what you're building. 该演示与您要构建的演示有很多相似之处。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM