2015-06-04 38 views
-4

我正在嘗試在服務工廠中獲取數據。沒有發生,它執行代碼。但沒有發生。不知道爲什麼?

enter image description here

這裏是我的服務代碼:

'use strict'; 
 

 
app.factory('accountService', function($http, $location, $rootScope) { 
 

 
    return { 
 
    accounts: function() { 
 
     $http.get('http://localhost:29045/AccountOperation.svc/GetAccounts').success(function(data, status, headers, config) { 
 
     console.log(status); 
 
     var $promise = data; 
 
     console.log(data); 
 

 
     }).error(function(data, status, headers, config) { 
 

 
     }); 
 
    } 
 
    } 
 
});

這裏是我的控制器(主叫從這裏出廠):

'use strict'; 
 
app.controller('accountController', function($scope, accountService, $rootScope) { 
 
    $scope.accounts = function() { 
 
    accountService.accounts(); 
 
    } 
 
});

此外我沒有得到錯誤。

+0

你曾經調用這個函數以下職位? – tymeJV

+1

請告訴我們您如何致電帳戶功能。 – Getz

+0

已更新的問題。 – fuat

回答

3

在您的帳戶功能上,您並未創建承諾或返回任何內容。嘗試:

app.factory('accountService', function($http, $location, $rootScope) { 

    return { 
    accounts: function() { 
     return $http.get('http://localhost:29045/AccountOperation.svc/GetAccounts'); 
    } 
    } 
}); 

這將返回承諾,你可以處理任何你調用acccount功能,或者你可以創建函數內承諾並返回。那麼在成功或錯誤方法內部解決或拒絕它。