UI-Router :我可以始终获得相同的控制器吗? [英] UI-Router : Can I get always the same controller?

查看:13
本文介绍了UI-Router :我可以始终获得相同的控制器吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 UI-Router 在我的应用程序中有一些菜单".

I'm using UI-Router to have some "menus" in my application.

$stateProvider.state("list"
    url: "/Focales"
    templateUrl: "/demo/focals.html"
    controller: FocalCtrl)
  $stateProvider.state("collection"
    url: "/Collections"
    templateUrl: "/demo/collections.html"
    controller: CollectionCtrl)

在我的 CollectionCtrl 中,我触发在服务器上完成的处理并等待显示这样的信息(CoffeeScript)

In my CollectionCtrl, I trigger a processing done on server and just waiting to display the information like this (CoffeeScript)

Progress = $resource('/collections/getProgress')

$scope.getProgress = () ->
    prg = Progress.get {}, ->
        $scope.currentProgress = prg
        $scope.getProgress() if prg.end isnt true

我的问题:当用户移动到 Focal 并返回到 CollectionCtrl 时,我有一个 CollectionCtrl 的新实例.所以据我所知,$scope.getProgress 代码仍然接收数据,但对于 PREVIOUS CollectionCtrl(所以显示没有更新......)

My issue : When the user moves to Focal and goes back to CollectionCtrl, I have a new instance of CollectionCtrl. So as far as I understand, the $scope.getProgress code still receives data but for the PREVIOUS CollectionCtrl (so the display is not updated...)

是否可以获得以前的 Controller 而不是 CollectionCtrl 的新实例"?为什么要创建一个新的 CollectionCtrl?什么是最好的方法:所以我很想拥有一个状态数据来存储当前的 $scope,这样我就可以执行 $state.currentScope = prg 而不是 $scope.currentProgress = prg.这是一个好方法吗?

Is it possible to get the previous Controller rather than a new "instance" of the CollectionCtrl ? Why is a new CollectionCtrl created ? What's the best approach: So I'm tempted to have a state data to store the current $scope so I could do $state.currentScope = prg rather than $scope.currentProgress = prg. Is it a good approach ?

谢谢!

推荐答案

最好的方法是定义一个 service 将与服务器通信并可能缓存结果.这个服务可以注入到控制器中,控制器可以向它询问 progress,可能使用 $watch,尽管为服务提供回调也可能就足够了,并更新基于此的用户界面.

The best approach to take here is to define a service which will communicate with the server and possibly cache the results. This service can be injected in the Controllers and the controllers can ask it for the progress, probably using $watch, though providing the service with a callback may also suffice, and update the UI based on that.

控制器应该是短暂的,不应依赖它们是 Angular 中的单例对象.重要的是,除了它们所支持的模板所需的状态之外,它们不应包含任何状态.当路由变得活跃时,它们会再次创建,这样我们就可以编写相对简单的代码,初始化 $scope 一次,并确保它包含 新鲜 信息(写入route) 作为模板.如果像 /app/:object_id 这样的每个路由都保留一个控制器并且用户经常切换路由,这也可以避免内存泄漏.

Controllers should be ephemeral and one should not rely on them being singleton-ish objects in Angular. Importantly, they should not contain any state apart from that needed by the template they are supporting. The are created again when the route becomes active so that we can write comparatively simpler code, initializing the $scope once and rest assured that it will contains as fresh information (w.r.t the route) for the template as possible. This also avoids memory leaks if a controller is kept around for each route like /app/:object_id and the user switches routes often.

这篇关于UI-Router :我可以始终获得相同的控制器吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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