UI-路由器:我能否获得总是相同的控制器? [英] UI-Router : Can I get always the same controller?

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

问题描述

我使用的UI路由器有一定的菜单我的应用程序。

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

我的问题:当用户移动到焦点,并可以追溯到CollectionCtrl,我有CollectionCtrl的新实例。所以,据我了解,在$ scope.getProgress code还是接收数据,但为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...)

是否有可能得到previous控制器,而不是CollectionCtrl的新实例?为什么一个新的CollectionCtrl产生的?
什么是最好的办法:所以我很想有一个状态数据存储当前$范围,所以我可以做$ 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 ?

谢谢!

推荐答案

在这里采取的最好的方法是定义的 服务将与服务器进行通信,并可能缓存结果。该服务可在控制器注入和控制器可以要求它为的进步的,可能使用 $观看,虽然提供的服务有回调也可能就足够了,和更新基于该用户界面

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.

控制器应该是短暂的,我们不应该依靠它们是在角单十岁上下的对象。重要的是,他们不应该包含除了由他们支持模板所需的任何状态。将再次创建时的路由变为主动,使我们可以写出相对简单的code,初始化 $范围一次,放心,这将包含为的新鲜的信息(WRT的路线)模板越好。这也避免了内存泄漏,如果控制器保持周围像 /应用/每条路线: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-路由器:我能否获得总是相同的控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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