【问题标题】:AngularJS pass data to template viewAngularJS 将数据传递给模板视图
【发布时间】:2015-10-16 11:52:37
【问题描述】:

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

$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 中显示模板。这是一个很长的文件,但它在

 &lt;div ng-controller="MarkerController"&gt;

并且有一些{{values}}。

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

我不能按照教程中的说明使用 ng-repeat,因为我创建的对象(不同的标记)与创建标记的控制器不同。我有一个服务返回 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[] 的元素数据绑定的视图。

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

【问题讨论】:

  • 请向我们展示两个不同对象/标记的示例(来自 markerTemplate),我们将向您展示如何正确使用 ngRepeat
  • 编辑帖子以在 JSON 中添加示例。

标签: javascript angularjs angular-leaflet-directive angularjs-templates


【解决方案1】:

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

我的控制器:

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

MyController 的视图:

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

【讨论】:

  • 我会这样做,但问题是,此视图需要位于标记的“消息”字段内。结果,我不能使用 ng repeat。这只是我希望显示此信息的一次,我只是有许多使用此模板的标记。 (或者我很困惑?)我正在尝试遵循这个:tombatossals.github.io/angular-leaflet-directive/examples/…(我只对模板标记感兴趣,而不是静态标记),但我想使用来自“MarkersAngularTemplateController”控制器的数据,而不是输入。它必须与每个标记相关。
【解决方案2】:

我刚遇到你的问题,我遇到了完全相同的问题。我所做的是在 ng-src div 中使用 ng-init,并为每个标记指定一个特定的 id,然后我可以使用它从范围中检索数据。例如这样的:

message: "<div ng-init='post=" + post.id + "'id='marker-data' ng-include src=\"'../templates/marker-message.html'\"></div>"

其中 post.id 是唯一标识符。现在我只是在我的标记模板文件中使用这样的东西。

{{posts[post]}}

获取我的独特帖子

【讨论】:

    猜你喜欢
    • 2011-02-20
    • 1970-01-01
    • 2014-12-30
    • 2013-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-19
    • 2019-02-16
    相关资源
    最近更新 更多