gpt4 book ai didi

javascript - AngularJS 将数据传递到模板 View

转载 作者:行者123 更新时间:2023-11-28 18:58:15 25 4
gpt4 key购买 nike

我正在使用 Angular-leaflet-directive,并且我想在单击标记时显示模板。我从网络服务器获取标记,并将它们存储在本地数组中。我创建标记的方式是:

$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]
}
});

它位于 for 循环内,迭代 DeviceInfoArray。

每个标记都在/templates/markerTemplate.html 中显示模板。这是一个很长的文件,但它位于

 <div ng-controller="MarkerController">

并且有一些{{values}}。

是否有某种方法可以绑定(bind)模板中的值或从 rootScope 或创建标记的 Controller 获取它们?

我无法按照教程中的说明使用 ng-repeat,因为我正在创建与创建标记的 Controller 不同的对象(不同的标记)。我有一个服务返回 DeviceInfo 中的数据,但我不想在每次单击标记时都查询它。

标记示例:

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>
该html是markerTemplate.html(我想要与devices[]的元素进行数据绑定(bind)的 View 。

devices[] 是一个名为mapController(和rootScope)的 Controller 内的一个数组,并且包含我想要显示的所有数据。我希望上面的模板从该数组的元素中获取值。

最佳答案

我认为你可以重构一下你的代码。在我看来,您不需要为每个标记包含一个新 Controller 。假设您有一个简单的 Controller MyController 和他的 View 模板。所以你可以尝试这样的事情:

我的 Controller :

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

MyController 的 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%;
}

关于javascript - AngularJS 将数据传递到模板 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33169849/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com