无法将数据从工厂返回到控制器AngularJS

问题描述 投票:1回答:1

我无法将数据从工厂方法返回到控制器。

service.js

var serverFac = angular.module('serverCom', []).factory('SmsSendService', function($http){

var transform = function(data){
    return $.param(data);
}

var temp = {};
temp.sendSms = function(tempJs){
    $http.post("http://localhost:8080/somepath/rest/text-messages/send/batch", tempJs ,{
        transformRequest: transform
    }).success(function(data, status, headers, config){
        //do stuff with response
    });
};

temp.login = function (tempUserPassword){
    $http.post("http://localhost:8080/somepath/rest/users/authenticate", tempUserPassword ,{
           transformRequest: transform
        }).success(function(data, status, headers, config){
            //do stuff with response
        }). error(function(data, status, headers, config){
            console.log(status);
        });
    };


temp.SimList = function (){
    $http.get("http://localhost:8080/RetireesClub_server/rest/sim-cards"
        ).success(function(data, status, headers, config){
            //do stuff with response

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

        });
};


    return temp;
});

serverFac.config(function($httpProvider){
    delete $httpProvider.defaults.headers.common['X-Requested-With'];
    $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-   urlencoded;charset=utf-8';
});

controller.js

function SimListController($scope, SmsSendService, SearchData, Search , $dialog , $location,$filter ){
    smsSendService.simList().success(function(data){
        $scope.sims= data;
    }
}
angularjs angularjs-factory
1个回答
2
投票

由于您正在控制器中处理成功回调,因此请让您的服务函数返回一个promise:

temp.SimList = function (){
    return $http.get("http://localhost:8080/RetireesClub_server/rest/sim-cards")
};
© www.soinside.com 2019 - 2024. All rights reserved.