2014-02-12 34 views
0

剛開始使用AngularJS。我不確定如何把這個搞混。我試圖在一個服務中包含多個功能。 (我希望這不是對不好的做法。)如何在AngularJS的服務中編寫多個Aync功能

以下是我的工作代碼:

myDataService.async().then(function (d) {    
     $scope.dbCalls = d.d;    
    }); 

我的服務:

app.factory('myDataService', function ($http) { 
// How do you get this bottom line to work? 
// this.getAllCalls = function() { 
    var myService = { 
     async: function() {     
      var promise = $http.post('AngularTest.aspx/FetchCalls', { data: {} }).then(function (response) { 
       console.log(response); 
       return response.data; 
      }); 
      return promise; 
     } 
    }; 
    return myService; 
//}; <--Commented out for clarity 
}); 

謝謝!

回答

0

你只是性質從服務返回一個對象,那麼你就可以調用這些屬性爲不同的服務方法

像這樣:

.service('myService', function() { 
    return { 
     firstMethod: function() { ... }, 
     secondMethod: function() { ... }, 
     thirdMethod: function() { ... } 
    } 
}) 

,並在控制器/指令

.controller('myCtrl', function(myService) { 
    myService.firstMethod(); 
    myService.secondMethod(); 
    myService.thirdMethod(); 
}) 
+0

精彩!!!這工作!謝謝! – InCode

相關問題