2017-04-17 150 views
0

我有一個控制器誰再延長控制器:控制器方法沒有返回值,延長控制器

我需要第二個控制器的方法的結果收到第一控制器的可變

第一個控制器:

angular.module('Gestios').controller('EmpresasControllerList', function($scope, $controller) { 
    angular.extend(this, $controller('AppController', { 
     $scope: $scope 
    })); 

    $scope.App = $scope.runApp('empresas', true); 

    console.log($scope.App); 
}); 

第二個控制器:

app.controller('AppController', function ($scope, $controller, AppFactory) { 
    $scope.runApp = function (AppPermalink, GetResults, Filters, Callback) { 
     if(!angular.isDefined(Filters)){Filters = $scope.Filters;} 

     var App = { 
      Load : { 
       busy: false, 
       page: 1 
      }, 
      Info: {}, 
      Results: [] 
     }; 

     AppFactory.Applications(AppPermalink).then(function (data) { 
      App.Info = data.data[0]; 

      if(GetResults){ 
       AppFactory.Items(AppPermalink, Filters).then(function (data) { 
        App.Results = { 
         list: data.data.data, 
         count: data.data.total 
        }; 

        if(typeof(Callback) == "function"){Callback(true, {results: App.Results.list, count: App.Results.count});} 

        return App; 
       },function() { 
        App.Results = { 
         list: [], 
         count: 0 
        }; 

        if(typeof(Callback) == "function"){Callback(false, {results: App.Results.list, count: App.Results.count});} 

        return App; 
       }); 
      } 
     },function (error) { 
      swal("Algo ha ido mal", error.error, "error"); 
     }); 
    }; 
}); 




}); 

http調用接收數據 ¿問題可能是由異步http調用引起的?

回答

0

用我的回調解決了這個問題:對

第一控制器:第二控制器

angular.module('Gestios').controller('EmpresasControllerList', function($scope, $controller) { 
    angular.extend(this, $controller('AppController', { 
     $scope: $scope 
    })); 

    $scope.runApp('empresas', true, false, function(Result, App){ 
     if(Result){ 
      $scope.App = App; 
     } 
    }); 
}); 

方法(AppController中):

$scope.runApp = function (AppPermalink, GetResults, Filters, Callback) { 
    if(!angular.isDefined(Filters)){Filters = $scope.Filters;} 

    var App = { 
     Load : { 
      busy: false, 
      page: 1 
     }, 
     Info: {}, 
     Results: [] 
    }; 

    AppFactory.Applications(AppPermalink).then(function (data) { 
     App.Info = data.data[0]; 

     if(GetResults){ 
      AppFactory.Items(AppPermalink, Filters).then(function (data) { 
       App.Results = { 
        list: data.data.data, 
        count: data.data.total 
       }; 

       if(typeof(Callback) == "function"){Callback(true, App);} 
      },function() { 
       App.Results = { 
        list: [], 
        count: 0 
       }; 

       if(typeof(Callback) == "function"){Callback(false, App);} 
      }); 
     } 
    },function (error) { 
     swal("Algo ha ido mal", error.error, "error"); 
    }); 
}; 
+0

有人考慮創建圍繞一個基於回調的包裝基於承諾的API將成爲反模式。請參閱[爲什麼是承諾回調'.then'方法反模式](http://stackoverflow.com/questions/35660881/why-are-callbacks-from-promise-then-methods-an-anti-pattern) 。 – georgeawg