0

I have a function calling an angular resource, to retrieve an item. I want the function to return the value received in the response. Anyway,

function getTemplate (templateId) {
    var Tx;
            Template.getTemplate({id:templateId})
                .$promise.then(
                  //success
                  function( response ){
                        Tx = response.response;
                        console.log (Tx);
                  },
                  //error
                  function( error ){
                    Tx = false;
                  }
               );
        return Tx
        }

When calling Template.getTemplate(123); I receive undefined, but I should get the response or false;

4

1 回答 1

0

请重构您的代码,如下所示,它可能不是确切的代码,但会对您有所帮助

function getTemplate (templateId,$q) {
    var deferred = $q.defer()
            Template.getTemplate({id:templateId})
                .$promise.then(
                  //success
                  function( response ){
                       deferred.resolve(response);
                  },
                  //error
                  function( error ){
                    deferred.fail("error");
                  }
               );
         return deferred.promise;
        }
于 2013-06-25T10:46:35.813 回答