简体   繁体   English

在angularJS中的两个视图之间共享数据

[英]share data between two Views in angularJS

I have 2 views,each one has a controller,I want to send data from View1 to View2 when I click on the "BL" Button: 我有2个视图,每个视图都有一个控制器,当我单击“ BL”按钮时,我想将数据从View1发送到View2:

this is View1: 这是View1: 在此处输入图片说明

and this is View2: 这是View2: 在此处输入图片说明

I tried to use the events my try ,but the Controller2 will be activated only when I click on the "BL" Button I thought to use a service to send Data from controller1 to controller2,but I think it's not my case so please is there any help,how can I send data from View1 to View2 thanks 我尝试使用尝试的事件,但是只有当我单击“ BL”按钮时,Controller2才会被激活,我认为该按钮使用一项服务将数据从controller1发送到controller2,但我认为不是我的情况,所以请在那里任何帮助,如何将数据从View1发送到View2,谢谢

Edit: this is my second try: 编辑:这是我的第二次尝试:

View1/controller1: 视图1 /控制器1:

 $scope.BLButton = function(){


    $scope.foo = Service.foo;
    Service.foo = '85';

    }
    .factory('Service', function() {
                                  var Service = {
                                        foo: '85'
                                      };
                                      return Service;
                                    });

View2/Controller2: View2 / Controller2:

//this method is called to display the data in the input Text from a web Service
$scope.parentmethod = function() {
  //traitement
$scope.foo = Service.foo;
....
}

the problem with my code is that the method "parentmethod" of Controleller2 is never called when I click on the "BL" button 我的代码的问题是,当我单击“ BL”按钮时,从未调用Controleller2的方法“ parentmethod”

The best approach is to move all you business logic (BL) to a service which will maintain app state. 最好的方法是将所有业务逻辑(BL)移至将维护应用程序状态的服务。 You're trying to misuse controllers in a way, because their only goal is to glue presentation and BL and not to hold app state in any form. 您正在尝试以某种方式滥用控制器,因为它们的唯一目的是粘合演示文稿和BL,而不是以任何形式保留应用程序状态。 The problem is that controllers are created and destroyed on view change, so they don't fit as state holders. 问题在于,控制器是在视图更改时创建和销毁的,因此它们不适合作为状态持有者。 In your case you can store data in a service (which is instantiated and alive all the time your app works) and then retrieve it in another view in controller. 在您的情况下,您可以将数据存储在服务中(该数据在您的应用运行期间一直被实例化并处于活动状态),然后在控制器的另一个视图中进行检索。

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

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