AngularJS工厂和控制器

时间:2014-05-31 09:17:57

标签: angularjs angularjs-scope angularjs-controller angularjs-factory

这里没什么问题,我有一个与工厂沟通的控制器,但我怎样才能将工厂结果传递给一个函数?我试过的东西:

.controller('testCtrl', ['$scope', 'foo', 'boo', function($scope, foo, boo){

     foo.get().then(function(response){
          $scope.foo = response;   
     });

     boo.get().then(function(response){
          $scope.boo = response;   
     });

     // Why this will not work?
     function test(){
          var getFoo = $scope.foo;
          var getBoo = $scope.boo; 
     };

}]

上面的示例不起作用,我该如何才能完成这项工作?

感谢。

1 个答案:

答案 0 :(得分:0)

对于参加聚会后的人们。一个不使用$scope的工作示例。

hoge.controller('testCtrl', ['$scope', 'foo', 'boo', function($scope, foo, boo){
  function test() {
     var getFoo = null;
     var getBoo = null; 

     Promise.all([foo.get(), boo.get()]).then(function(results) {
       getFoo = results[0];
       getBoo = results[1];
     });
   };
}]