How to return a resolved promise from an AngularJS Service using $q?

后端 未结 7 1664
Happy的楠姐
Happy的楠姐 2020-12-05 17:23

My service is:

myApp.service(\'userService\', [
  \'$http\', \'$q\', \'$rootScope\', \'$location\', function($http, $q, $rootScope, $location) {
    var defe         


        
7条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-05 17:35

    From your service method:

    function serviceMethod() {
        return $timeout(function() {
            return {
                property: 'value'
            };
        }, 1000);
    }
    

    And in your controller:

    serviceName
        .serviceMethod()
        .then(function(data){
            //handle the success condition here
            var x = data.property
        });
    

提交回复
热议问题