使用 $resource 时如何等到 promise returns

how to wait until promise returns when using $resource

我有一个带有激活方法的控制器,该方法在继续之前等待 return 的承诺。

        function activate() {
        var promises = [getNodesGroup(), getNodes()];
        common.activateController(promises, controllerId)
            .then(function () {
                log('Activated');
            });
    }

这两个承诺都有类似的方法来获取数据,并且在 datacontext.getNodeGroups() 和 datacontext.getNodes() methods.one 中使用 $http 就像这样

        function getNodesGroup() {
        return datacontext.getNodeGroups().then(function (response) {
            $scope.nodeGroups = response.data;
           //other logic
        });

直到现在,当我尝试将 $resource 用于其中一个承诺 (getNodes()) 时,一切都运行良好。我的节点资源是这样设置的

(function () {
'use strict';
var nodeServiceRoot = '/api/Nodes';

var GetAllNodesUrl = nodeServiceRoot + '/GetAllNodes';

angular.module('common.service')
.factory("nodeResource",["$resource","appSettings", nodeResource])
function nodeResource($resource, appSettings) {
    return $resource(appSettings.serverPath + GetAllNodesUrl);
}}());

我正在尝试这样消费它

        function getNodes() {
        $scope.nodes = nodeResource.query();
        $scope.nodes.$promise.then(function (response) {
            $scope.nodes = response;
            //other logic
        });

我不知道如何 return getNodes() 以便我的激活函数在继续之前等待它。现在发生的事情是,有时两个函数在遇到 log('Activated') 时都是 运行;激活函数行,有时只有一个使用旧 $http 方法 (getNodeGroups()) 的承诺是 运行 而不是另一个。

好吧,我想答案很简单。我在 getNodes() 中丢失了 'return' 所以对我有用的是

 function getNodes() {
    $scope.nodes = nodeResource.query();
  **return**  $scope.nodes.$promise.then(function (response) {
        $scope.nodes = response;
        //other logic
    });