如何将 $scope.variables 传输到控制器? [英] How to transfer $scope.variables to controller?

查看:23
本文介绍了如何将 $scope.variables 传输到控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个系统,其中我将项目列在带有变量的列表中

I am building a system where i have items listed in a list with variables

  1. 标题
  2. 技能
  3. 预算
  4. 已发布

我还有一个模态,当我点击列表中的一个项目时它会打开.我想获取当前选定的项目并在我的第二个模态控制器中使用其变量?这可能吗?

I also have a Modal that opens up when i click on an item in the list. I would like to take the current selected item and use its variables in my second modal controller? Is this possible?

我正在考虑使用一种服务来缓存从数据库中提取的所有数据,但是我不知道如何只显示选定的项目而不是模式中的每个项目.

I was thinking of using a service where i cache all data that is pulled from database, but then i dont know how i would only display selected item and not EVERY item in the modal.

这是我的列表项控制器:

Here is my controller for the list items:

app.controller('openProjectsCtrl', ['$scope', '$http', 'projectsModal',
function ($scope, $http, projectsModal) {

  $http.get("http://localhost/app/controllers/php/getProjects.php")
  .success(function (response) {
    $scope.projects = response.projects;
  });

  $scope.showModal = function() {
      projectsModal.deactivate();
      projectsModal.activate();
  };    

}]);

模态控制器(我想在哪里查看选定的变量):

Modal Controller(Where i want to view the Selected Variables):

app.controller('projectsModalCtrl', ['$scope', '$timeout', 'projectsModal',
function ($scope, $timeout, projectsModal) {

var ctrl = this;

ctrl.closeMe = function () {
  projectsModal.deactivate();
};

}]);

推荐答案

是的,您可以使用两种方法在另一个控制器中使用一个控制器的变量

Yes you can use variables of one controller inside another controller using two methods

  1. 创建服务以在它们之间进行通信.
  2. 使用 $rootScope.$broadcast

示例代码

angular.module('myservice', []).service('msgBus', function() {
        this.serviceValue= {};

    }]);
});

并像这样在控制器中使用它:

and use it in controller like this:

控制器 1

angular.module('myservice', []).controller('ctrl1',function($scope, msgBus) {
    $scope.sendmsg = function() {
        msgBus.serviceValue='Hello'; 
   }
});

控制器 2

angular.module('myservice', []).controller('ctrl2',function($scope, msgBus) {
$scope.checkValue(){   
alert( msgBus.serviceValue);
}
});

这篇关于如何将 $scope.variables 传输到控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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