K. Gkinis K. Gkinis - 6 months ago 19
AngularJS Question

AngularJS pass data to template view

I'm using angular-leaflet-directive, and I want to display a Template when a marker is clicked. I'm getting the markers from a web server, and storing them locally in an array. the way that I'm creating the markers is:



$rootScope.markers.push({
lat: parseFloat(DeviceInfo[i].Position.Y),
lng: parseFloat(DeviceInfo[i].Position.X),
message: "<div ng-include src=\"'templates/markerTemplate.html'\"></div>",
icon: {
iconUrl: url,
iconSize: [39, 39],
iconAnchor: [19, 19],
popupAnchor: [-19, -19]
}
});





which is inside a for loop, that iterates over the DeviceInfoArray.

Every marker is displaying the template in /templates/markerTemplate.html . This is a long file, but it's under a



<div ng-controller="MarkerController">





and has some {{values}}.

Is there some way to bind the values in the template or get them from the rootScope or the controller that creates the markers?

I can't use ng-repeat as instructed in the tutorial, because I'm creating different objects (different markers) than the controller creating the markers. I have a service returning the data in DeviceInfo, but I'd rather not query it every time a marker is clicked.

Example markers:



marker1: {
lat: 50,
lng: 20,
message: "<div ng-include src=\"'templates/markerTemplate.html'\"></div>",

icon: {
iconUrl: url,
iconSize: [39, 39],
iconAnchor: [19, 19],
popupAnchor: [-19, -19]
}
}

$scope.Devices[1].deviceData: {
LastUpdate: "01/01/2015 12:14",
Position: {
X: 50,
Y: 20
},
Speed: 30,
DeviceIdentifier: "DeviceName1"
}

marker2: {
lat: 55,
lng: 25,
message: "<div ng-include src=\"'templates/markerTemplate.html'\"></div>",

icon: {
iconUrl: url,
iconSize: [39, 39],
iconAnchor: [19, 19],
popupAnchor: [-19, -19]
}
}

$scope.Devices[2].deviceData: {
LastUpdate: "02/23/2015 13:14",
Position: {
X: 55,
Y: 25
},
Speed: 45,
DeviceIdentifier: "DeviceName2"
}

<div ng-controller="markerController">
<font style="font-size:12px; font-weight:bold">{{deviceData.DeviceIdentifier}}</font>
<table width="100%">
<tbody>
<tr>
<td><strong>Speed: </strong>
</td>
<td>{{deviceData.Speed}} km/h</td>
</tr>
<tr>
<td><strong>Update: </strong>
</td>
<td>{{deviceData.LastUpdate}}</td>
</tr>
<tr>
<td colspan="2">
<hr>
</td>
</tr>
</tbody>
</table>
<div><strong>Location: </strong>{{deviceData.Location}} (X:{{deviceData.Position.X}}Y:{{deviceData.Position.Y}})
</div>
</div>




The html is the markerTemplate.html (the view that I want to data bind with the elements of devices[].

devices[] is an array inside a controller called mapController (and the rootScope), and has all the data that I want to display. I want the above template to get values from the elements of this array.

Answer

I think that you could refactor a bit your code. In my opinion you don't need a include with a new controller for each marker. So let's say that you have a simple controller MyController and his view template. So you could try something like this:

MyController:

$scope.markers.push({
  device: DeviceInfo[i],
  icon: {
    iconUrl: url,
    iconSize: [39, 39],
    iconAnchor: [19, 19],
    popupAnchor: [-19, -19]
  }
});

MyController's View:

<div class="my-marker" ng-repeat="marker in markers">
  <b>{{marker.device.deviceData.DeviceIdentifier}}</b>
  <table>
    <tbody>
      <tr>
        <td><strong>Speed: </strong>
        </td>
        <td>{{marker.device.deviceData.Speed}} km/h</td>
      </tr>
      <tr>
        <td><strong>Update: </strong>
        </td>
        <td>{{marker.device.deviceData.LastUpdate}}</td>
      </tr>
      <tr>
        <td colspan="2">
          <hr>
        </td>
      </tr>
    </tbody>
  </table>
  <div><strong>Location: </strong>{{marker.device.deviceData.Location}} (X:{{marker.device.deviceData.Position.X}}Y:{{marker.device.deviceData.Position.Y}})
  </div>
</div>

CSS:

.my-marker b{
  font-size: 12px;
}
.my-marker table{
  width: 100%;
}
Comments