0

To provide a client to my angular application, here is my code:

app.service('client', function (esFactory) {
        var elasticserver='elasticHost:9200';

        return esFactory({
            host: elasticserver,
            log: 'warning'
        });
    }
);

But my purpose is to provide dynamically the host name and port to this service.

So how to achieve that ?

Thanks

UPADTE

the server name and port are stored in database. so I was trying to get these values through an http call.

below the code.

at line 6 the elasticserver variable contains the concatenation of the servername and port but at line 11 the elasticserver variable is empty.

app.service('client', function (esFactory, $http) {
    var elasticserver = "";
    $http.get("angularAction").success(
            function(data) {
                elasticserver = data['serverName']+":"+data['port'];
                alert("elasticserver : "+elasticserver);
            }).error(function(data, status, headers, config) {
                            // called asynchronously if an error occurs
                            // or server returns response with an error status.
    });
    alert("elasticserver : "+elasticserver);
    return esFactory({
        host: elasticserver,
        log: 'warning'
    });
}
);
kkung
  • 715
  • 4
  • 10
  • 18
  • 1
    dynamically … in response to what? – Quentin Nov 09 '16 at 21:38
  • Duplicate: http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – Quentin Nov 10 '16 at 10:06
  • Thanks Quentin I understand now why it does'nt work for me. could you provide me an exemple of solution to this case using callbacks or promises ? – kkung Nov 10 '16 at 15:11

1 Answers1

0

If your code is working but you need to set your server dynamically you can concatenate hostname and port.

let port = 9200;
let hostName = elasticHost;

app.service('client', function (esFactory) {
        var elasticserver= hostName + ':' + port;

        return esFactory({
            host: elasticserver,
            log: 'warning'
        });
    }
);
snnsnn
  • 10,486
  • 4
  • 39
  • 44