Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/427.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在AngularJS服务中编写多个Aync函数_Javascript_Angularjs - Fatal编程技术网

Javascript 如何在AngularJS服务中编写多个Aync函数

Javascript 如何在AngularJS服务中编写多个Aync函数,javascript,angularjs,Javascript,Angularjs,我刚开始学习AngularJS。我不知道该怎么做。我试图在一个服务中包含多个功能。(我希望这不是反对不良做法。) 以下是我的工作代码: myDataService.async().then(function (d) { $scope.dbCalls = d.d; }); 我的服务: app.factory('myDataService', function ($http) { // How do you get this

我刚开始学习AngularJS。我不知道该怎么做。我试图在一个服务中包含多个功能。(我希望这不是反对不良做法。)

以下是我的工作代码:

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
});
app.factory('myDataService',函数($http){
//你如何让这个底线发挥作用?
//this.getAllCalls=函数(){
var myService={
异步:函数(){
var promise=$http.post('AngularTest.aspx/FetchCalls',{data:{}})。然后(函数(响应){
控制台日志(响应);
返回响应数据;
});
回报承诺;
}
};
返回myService;

//}您只需从服务返回一个带有属性的对象,然后就可以将这些属性作为不同的服务方法调用

像这样:

.service('myService', function() {
    return {
        firstMethod: function() { ... },
        secondMethod: function() { ... },
        thirdMethod: function() { ... }
    }
})
在控制器/指令中

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