0

I am stuck here. what's wrong with my code? i can't get the value of the variable when I return it.

accountManagers : function( id ) {

        var accountManager;

        $http.get( URL + 'server/agents_account_managers.php?id=' + id ).success( function ( data ) {

            if ( data != 0 ) {

                accountManager = data[0].first_name + ' ' + data[0].last_name ;

            }

        });     

        return accountManager;


    }

thanks

  • thanks for the reply, i checked the link and found there's a lot of explanation. i am having a hard time to understand it all. could you please give me a summary or simplified solution to this? thanks –  Feb 18 '16 at 04:28
  • Please refer this link http://stackoverflow.com/questions/19811655/success-call-back-function-from-angularjs – Pratik Bhajankar Feb 18 '16 at 05:56
  • thanks for the link, my question is my function is in the service. will $q work in service? thanks again –  Feb 18 '16 at 06:56
  • i already tried it, but it showed me this : d {$$state: Object} –  Feb 18 '16 at 07:07

1 Answers1

0

Try using then().

$http.get('/yoururl').then(
  function(response) { // successCallback
    console.log('success: ' + response);
  }, function (error) {  // errorcallback
    console.log('error: ' + error);
  });

This uses Angular's $http shortcut methods, and will handle any errors in the errorcallback method.

You should also avoid using .success and .error when using $http

The $http legacy promise methods success and error have been deprecated. Use the standard then method instead.

sjokkogutten
  • 2,005
  • 2
  • 21
  • 24
  • can you please check what's wrong with this... –  Feb 18 '16 at 09:09
  • var crmGlobalServices = angular.module( 'crmGlobalServices', [] ); crmGlobalServices.factory( 'crmGlobalServices', function( $http, $q ) { var accountManager; function agentsAccountManager( id ) { $http.get( URL + 'server/agents_account_managers.php?id=' + id ).then( function ( response ) { console.log( response.data[0].first_name ); // already success accountManager = response.data[0].first_name + ' ' + response.data[0].last_name; }, function ( error ) { console.log( 'error : ' + error ); }); console.log( accountManager ); return accountManager; } }); –  Feb 18 '16 at 09:11
  • You should probably update your answer with a plunker – sjokkogutten Feb 18 '16 at 09:43