1

I am having trouble returning data from a factory method to a controller.

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;
    }
}
5
  • what version of angular? Commented Sep 12, 2013 at 14:14
  • Well, I used to use $rootScope.$broadcast('you.event', you.data) method to send data to a scope. And in controller you can write $scope.$on('you.event', function (evt, data) { ... }) Actually, in my opinion this is one of the best practice. Thus, your modules are totally separated. Commented Sep 12, 2013 at 14:16
  • angularjs 1.0.7 @DanKanze Commented Sep 12, 2013 at 14:18
  • Are you missing some code, where is the function simList()? Commented Sep 12, 2013 at 14:18
  • oh , sorry , i update code @Mark Commented Sep 12, 2013 at 14:23

1 Answer 1

2

Since you are handling the success callback in the controller, make your service function return a promise:

temp.SimList = function (){
    return $http.get("http://localhost:8080/RetireesClub_server/rest/sim-cards")
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.