本文介绍了采用了棱角分明的服务共享控制器之间的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有两个不同的控制器相互通信。

I am trying to have two different controllers communicate with each other.

控制器1

function WelcomeCtl($scope, emailService) {
    $scope.save=function(){
        emailService.saveEmail(‘Hi’);
        }
}

WelcomeCtl.$inject = [$scope, emailService];

本控制器的设计采取从文本字段中的文本(含伍模型=电子邮件),并把文成服务(emailService),所以它可以在未来的NG-视图中使用(这是受控由下一个控制器)
//用于测试目的,我只是把'嗨'直接进入saveEmail功能

This controller is designed to take the text from a text field (with ng-model = ‘email’) and put the text into a service (emailService) so it can be used in the next ng-view (which is controlled by the next controller)//for testing purposes I am just putting ‘Hi’ directly into the saveEmail function

2控制器

function SignupCtl($scope, emailService) {                                           
    window.alert(emailService.getEmail())
}

SignupCtl.$inject = [$scope, emailService];

为了测试意我使用window.alert显示getEmail()

emailService

angular.module('myApp.services', [])
       .service('emailService', function (){
           var text ="start value";
           return{
               saveEmail:function (data){
                  text  = data;

              },
              getEmail:function (){
                  return text;
              }
          };
      });

由于与该服务,window.alert打印出启动值,而不是指定使用控制器1和控制器2的结果嗨

As a result of using Controller1 and Controller 2 as specified with this service, window.alert prints out "start value" instead of ‘Hi’

当我把更多的window.alert功能,在code,看看发生了什么我看到,当在save()被调用时,控制器1执行并保存一个值到服务和前任press.js加载下一页。然后,控制器2激活。当控制器2激活它重新初始化服务,将文本回启动值。然后,当getEmail叫其返回值启动值

When I put more window.alert functions in the code to see what is happening I see that when save() is called, Controller 1 executes and saves a value into the service and express.js loads the next page. Then Controller2 activates. When Controller 2 activates it reinitializes the service, setting text back to "start value". Then when getEmail is called it return the value "start value"

这混淆了我,因为我是那个服务是每次被列入控制器的时间未初始化的IM pression下。

This confuses me because I was under the impression that services were not initialized every time the were included in a controller.

请教了资源。结果
Better设计用于将数据传递到其他NG-观点在AngularJS。跨控制器坚持它

我也工作过的角-EX preSS-种子
<一href=\"https://github.com/btford/angular-ex$p$pss-seed\">https://github.com/btford/angular-ex$p$pss-seed

推荐答案

Angular.js只适用并保持在一个页面上的数据。如果你的页面重新加载(如你似乎表明,当你说恩press.js加载下一页,然后将其重新初始化一切。

Angular.js only works and keeps data on a single page. If your page reloads (as you seem to indicate when you say "express.js loads the next page", then it reinitialized everything.

您应该:​​


  • 看看如何使用Angular.js路由( ),使您保持在同一页上。

  • 使用的东西,以保存数据,如的localStorage 。了解更多:

  • look at how to use Angular.js routing (http://docs.angularjs.org/tutorial/step_07) so that you stay on the same page.
  • use something to persist your data, such as localstorage. Find out more: http://diveintohtml5.info/storage.html

这篇关于采用了棱角分明的服务共享控制器之间的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 17:54