.factory必须从$ get factory方法返回一个值

时间:2017-11-09 07:17:31

标签: angularjs

请帮我解决我在这里做的错误

factory.js

angular.module('MyApp').factory('MilkFactory', function ($http) {
    var GeneralService = {};
    GeneralService.DoCal = function () {
        return $http({
            url: 'http://localhost:10948/Api/Home/GetEmployee',
            method: 'Get',
            contentType: 'application/x-www-form-urlencoded'

EmpCtrl.js

angular.module('MyApp')
.controller('EmployeeController', function ($scope, EmployeeService, $q, MilkFactory) {
$scope.GetFactory = function () {
        MilkFactory.DoCal().then(function () {
            alert('Milk is calling...')
            return 
        })
    }

1 个答案:

答案 0 :(得分:1)

您需要从工厂返回服务对象

angular.module('MyApp').factory('MilkFactory', function ($http) {
    var generalService = {};
    generalService.DoCal = function () {
        return $http({...});
     };
     return generalService;
 });