【问题标题】:Two controllers, calling functions from each other两个控制器,互相调用函数
【发布时间】:2015-11-12 09:38:52
【问题描述】:

我正在处理 SharePoint 中的两个列表,因此在我的项目中我有三个控制器,一个主控制器 控制器,每个 SharePoint 列表一个。 我的清单是项目和任务。一个项目可以有多个任务。

当我想查看一个项目时,在 itemList 控制器中我这样做:

$scope.loadItem = function(id){
    var promise = ItemService.getItem(id);
    promise.then(function(item){
        $scope.viewItem(item);
    }, function(err){console.log(err);})
};

然后,当我想加载任务时,我在 taskList 控制器中有这个

$scope.loadTasks = function(parentId){
    var promise = TaskService.getTasks(parentId);
    promise.then(function(result){
        console.log(result);
    }, function(err){console.log(err);})
};

问题来了,当我加载我的项目 $scope.loadItem() 时,我还想加载相关的任务 到该项目,但该功能在另一个控制器中(以保持清洁)。

这就是我认为的样子:

 $scope.loadItem = function(id){
    var promise = ItemService.getItem(id);
    promise.then(function(item){
        $scope.loadTasks(item.Id);
        $scope.viewItem(item);
    }, function(err){console.log(err);})
};

但我只能通过两种方式实现这一点,那就是将我的 loadTasks 函数移动到 与 loadItems 函数或主控制器相同的控制器(因为它们都继承自该控制器), 但这会弄乱我的代码。

如何从我的 itemList 控制器调用 loadTasks 函数,而不会在错误的控制器中创建错误的函数,从而弄乱我的代码?

【问题讨论】:

  • 也许创建一个服务来处理请求并且所有控制器都可以访问?
  • Adwaenyth 是对的。您应该使用服务。

标签: angularjs


【解决方案1】:

我认为你可以使用$on$broadcast。像下面这样;

在 itemList 控制器中;

$scope.loadItem = function (id) {
    var promise = ItemService.getItem(id);

    promise.then((function (item) {
        $scope.viewItem(item);

        $rootScope.$broadcast('loadTasks', item.id);
    }, function (err) { console.log(err); });
};

在taskList控制器中;

$scope.loadTasks = function (parentId) {
    var promise = TaskService.getTasks(parentId);

    promise.then(function (result) {
        console.log(result);
    }, function (err) {console.log(err);})
};

$rootScope.$on('loadTasks', function (event, id) {
    $scope.loadTasks(id);
});

也许您可以使用$emit 代替$broadcast$emit 在性能上优于 $broadcast。您可以在this answer 上找到更多信息。如该答案所述,当您的本地 $scope 被销毁时,您应该销毁该侦听器。

即使您愿意,也可以使用服务来传递数据。

【讨论】:

  • 这样,当我调用loadItem函数时,它会广播Id,并运行loadTasks函数?这看起来不错,$on 和 $broadcast 有什么缺点吗?我以前没见过很多..
  • @klskl;我为您的评论更新了我的答案。我希望它适合你。
【解决方案2】:

您可以创建一个服务来实现此目的:

angular
  .module('myApp', [])
  .factory('shareService', shareService)

  function shareService() {
    var memory = {};
    return {
        store: function (key, value) {
            memory[key] = value;
        },
        get: function (key) {
            return memory[key];
        }
    };  
  }

然后,在您的控制器中,您必须:

// Controller 1
function FirstCtrl ($scope, shareService) {
  shareService.store('FirstCtrl', $scope)
  $scope.variableOne = 'First Controller Variable';
}

// Controller 2 (calling func from Controller 1)
function SecondCtrl ($scope, shareService) {
  $scope.getOne = function() {
    $scope.resOne = shareService.get('FirstCtrl').variableOne;
  }
}

这是plunker

【讨论】:

  • 我理解您要解释的内容,但是当第一个函数运行时,我的第二个函数将如何运行?我的 loadTasks 函数会在该服务中吗?还是服务会调用它?
  • 您可以将其存储在服务中,并在需要时调用它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-23
相关资源
最近更新 更多