0

I am trying to add a variable inside the path to map layer.

mypath='MyApp'
    var Finals = L.tileLayer.wms("http://localhost:8080/geoserver/'+mypath+'/wms", {
        layers: 'MyApp:finalVector',
        format: 'image/png',
        transparent: true,
        version: '1.1.0',
        attribution: "myattribution"
    });

How can I add mypath variable inside the Finals variable.

After adding it should act like following

var Finals = L.tileLayer.wms("http://localhost:8080/geoserver/MyApp/wms", {
        layers: 'MyApp:finalVector',
        format: 'image/png',
        transparent: true,
        version: '1.1.0',
        attribution: "myattribution"
    });
Richiedlon
  • 111
  • 1
  • 4
  • 13
  • Change `'+mypath+'` to `"+mypath+"` – Jrey Oct 24 '17 at 04:11
  • Possible duplicate of [JavaScript Variable inside string without concatenation - like PHP](https://stackoverflow.com/questions/3304014/javascript-variable-inside-string-without-concatenation-like-php) – Phani Kumar M Oct 24 '17 at 04:21

2 Answers2

1
mypath='MyApp'
    var Finals = L.tileLayer.wms("http://localhost:8080/geoserver/" + mypath + "/wms", {
        layers: 'MyApp:finalVector',
        format: 'image/png',
        transparent: true,
        version: '1.1.0',
        attribution: "myattribution"
    });
Pratheesh M
  • 1,028
  • 6
  • 13
1

You can use template literals to pass variable to string.

var Finals = L.tileLayer.wms(`http://localhost:8080/geoserver/${mypath}/wms`, {
  layers: 'MyApp:finalVector',
  format: 'image/png',
  transparent: true,
  version: '1.1.0',
  attribution: "myattribution"
});
Syed
  • 269
  • 2
  • 8
  • **template literals** is an interesting thing, do we need to add any new js library for it to work? – Yogesh H Shenoy Oct 24 '17 at 04:23
  • Refer [this](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) to know more about it. – Syed Oct 24 '17 at 04:26