通过控制器之间的数据 [英] pass data between controllers

查看:136
本文介绍了通过控制器之间的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我指出来学习AngularJS,从很多不同的MV *框架的到来。
我喜欢的框架,但我在与控制器之间传递数据的麻烦。

I'm stating to learn AngularJS, coming from a lot of different MV* frameworks. I like the framework, however I'm having trouble with passing data between Controllers.

假如我有一些输入(input.html)和控制器的屏幕,让我们说InputCtrl。

有一个关于这个观点这需要你到另一个画面中的按钮,让我们说同意(approve.html)与控制器ApproveCtrl。

这ApproveCtrl从InputCtrl需要的数据。这似乎是在较大的应用中非常常见的场景。

Suppose I have a screen with some input (input.html) and a controller, let's say InputCtrl.
There's a button on this view which takes you to another screen, let's say approve (approve.html) with a controller ApproveCtrl.
This ApproveCtrl needs data from the InputCtrl. This seems like a very common scenario in bigger applications.

在我的previous MV *框架,这会像处理(伪code):

In my previous MV* frameworks, this would be handled like (pseudo-code):

   var self = this;
   onClick = function() {
          var approveCtrl = DI.resolve(ApproveCtrl);
          approveCtrl.property1 = self.property1;
          approveCtrl.property1 = self.property2;
          self.router.show(approveCtrl);  
   }            


  • 它的工作就像控制器 - 第一位。
    首先创建控制器,有一个机会,把它在正确的状态;事后查看被创建。

  • 现在,在AngularJS,我处理这种类似:

    Now, in AngularJS, I'm handling this like:

     var self = this;
     onClick = function(){
              self.$locationService.path('approve');       
     }
    


    • 这就像浏览一。
      你说哪个视图/路线导航,控制器获取由框架创建。

    • 我觉得很难控制所创建的控制器的状态和数据传递到它。
      我见过,并试图下列办法,但都具有它在我认为自己的问题:

      I find it hard to control the state of the created Controller and pass data to it. I've seen and tried following approaches, but all have it's own issues in my opinion:


      1. 注入一个共享服务为InputCtrl&功放; ApproveCtrl并把所有的数据对这一服务进行共享

        • 这看起来像一个肮脏的解决方法;在共享服务状态变为全局状态,而我只需要它的数据传递给ApproveCtrl

        • 此共享服务的生命周期比我需要它的方式不再 - 只是将数据传递给ApproveCtrl


      • 这具有通过大量的参数时变得相当混乱


      • 从概念上讲,这不是我会用事件 - 我只是需要将数据传递到ApproveCtrl,没有什么事件杂交

      • 这是相当麻烦;我必须先发送一个事件到父,那么这将广播给它的孩子

      我失去了一些东西在这里?我是不是创造太多的小控制器?
      我在试图抓住从其他框架太多了这里的生活习惯?

      Am I missing something here? Am I creating too many small Controllers? Am I trying to hold on to habits from other frameworks too much here?

      推荐答案

      在结构AngularJS而言是更加模块化比MVC之一。

      In terms of structure AngularJS is more Modular than MVC one.

      经典MVC描述了彼此在这样的方式进行交互控制器与拆线视图模型(与模型不应该相当与视图直接或反之亦然工作)3个简单的图层。

      Classic MVC describes 3 simple layers which interact with each other in such way that Controller stitches Model with View (and Model shouldn't rather work with View directly or vice versa).

      在角可以有多个,一些完全可选的,它可以以多种方式彼此之间进行交互的实体,例如:

      In Angular you can have multiple, some completely optional, entities which can interact between each other in multiple ways, for example:

      这就是为什么有不同的实体之间的通信数据的多种方式。您可以:

      That's why there are multiple ways of communicating your data between different entities. You can:

      • Send messages directly between controllers using difference between this and $scope
      • Send messages using events
      • Send messages using shared system (Note: same link as above, answer shows both techniques)

      • Send messages using AJAX backend
      • Send messages using external system (such as MQ)

      ......以及更多。由于它的多样性角度允许开发/设计人员选择方式,他们最熟悉和发扬。我建议你​​阅读 AngularJS开发指南在这里你可以找到的祝福的解决一些常见问题。

      ...and a lot more. Due to its diversity Angular allows developer/designer to choose way they are most comfortable with and carry on. I recommend reading AngularJS Developer Guide where you can find blessed solutions to some common problems.

      这篇关于通过控制器之间的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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