使用合格的服务和混乱控制器之间的数据使用['$范围','服务',函数($范围,服务){}] [英] Passing data between controllers using service and confusion with using [ '$scope' , 'service' , function($scope, service){}]

查看:163
本文介绍了使用合格的服务和混乱控制器之间的数据使用['$范围','服务',函数($范围,服务){}]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我严重套牢在angularJS从一个控制器将数据传递到其他的这个问题。我的code是前:每当我点击templateController的分区,它会引发setTemplate的帮助下NG点击...现在我的目标是templateController的选择数据发送到ReplyController ...

I am badly stuck with this problem of passing data from one controller to other in angularJS. Before my code was: whenever I click on templateController's div, it would trigger setTemplate with the help of ng-click... Now my objective was to send templateController's selected data to ReplyController...

在这里读书论坛的帖子后,我决定创建一个名为selectionService服务,让我可以在2个控制器之间传输数据...

After reading forum posts here, i decided to create a service called 'selectionService', so that i can transmit data between 2 controllers...

//Defined Service

proApp.service('selectionService', function() {
  var selected_template;

  addTemplate = function(newObj) {
      selected_template = newObj;
  };
  getTemplate = function(){
      return selected_template;
  };
});

//Controller 1... where templates are selected

proApp.controller('TemplateController',function($scope, selectionService)
{
    $scope.templates = [ 
    {template_name:"Template 1", template_text:"We apologize for the interruption."} ,
    {template_name:"Template 2", template_text:"Thank you for contacting us."} ,
    } ,
    ];

    // on ng-click
    $scope.setTemplate = function(tmp)
    {
        selectionService.addTemplate(tmp);   
    }
});


// Controller 2 ... supposed to catch the selected template.

proApp.controller('ReplyController', function($scope, selectionService) 
{
    $scope.template = selectionService.getTemplate();
});

所以,每当我运行此code,我开始收到此错误

So whenever i run this code, i started getting this error

Object [object Object] has no method addTemplate...

我再次tweeked的code在那里他们被建议使用Squarebrackets并把$范围,服务名,然后写有相同参数的功能。我不明白为什么我应该这样做?做这样的一些变化后,事件
  ['$范围','服务',函数($范围,服务){}]

我仍无法找出解决方案,以从​​一个控制器将数据传递给其他使用的服务。

I am still not able to figure out the solution to pass data from one controller to other using service.

你能帮忙吗?我在想什么?我是很新的东西,做的angularJS方式。

Could you help? What am I missing? I am very new to angularJS way of doing stuff.

推荐答案

我觉得它其实很简单。你只需要使用来增加您的方法来服务这一点。。目前,他们被宣布窗口。改变你的服务声明使用这个 ...

I think it's actually quite simple. You just need to add your methods to the service by using this.. Currently they are declared on window. Change your service declaration to use this...

proApp.service('selectionService', function() {
  var selected_template;

  this.addTemplate = function(newObj) {
      selected_template = newObj;
  };
  this.getTemplate = function(){
      return selected_template;
  };
});

至于使用数组符号的依赖,这是一个很好的做法,但它不是必需的。它会从头痛救你,如果你曾经通过minifier运行code。

As for using the array notation for dependencies, it's a good practice but it's not required. It'll save you from headaches if you ever run your code through a minifier.

这篇关于使用合格的服务和混乱控制器之间的数据使用['$范围','服务',函数($范围,服务){}]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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