包含的 HTML 模板中的 AngularJS id 属性
AngularJS id attribute in the included HTML template
在 AngularJS 中,我的 JavaScript 无法识别 HTML 模板中的 id 属性。
例如:
索引.html文件 :
<div ng-controller="SubwayMapController">
<div subway-map></div>
</div>
脚本.js文件 :
app.directive('subwayMap', function () {
return {
templateUrl: 'Views/subway-map.html'
};
});
map = new OpenLayers.Map("basicMap");
var mapnik = new OpenLayers.Layer.OSM();
var fromProjection = new OpenLayers.Projection("EPSG:4326"); // Transform from WGS 1984
var toProjection = new OpenLayers.Projection("EPSG:900913"); // to Spherical Mercator Projection
var position = new OpenLayers.LonLat(2.35,48.85).transform( fromProjection, toProjection); //Paris
var zoom = 12;
map.addLayer(mapnik);
map.setCenter(position, zoom );
地铁地图.html文件 :
<div id="basicMap"></div>
当我打开索引页面时,我希望显示来自Openstreetmap的地图,但没有任何反应。
尝试在指令的link()
函数中设置映射。另外,看起来您可以使用DOMElement Map.render()
,而不需要ID。
app.directive('subwayMap', function () {
return {
templateUrl: 'Views/subway-map.html',
link: function(scope, element) {
var map = new OpenLayers.Map();
var mapnik = new OpenLayers.Layer.OSM();
var fromProjection = new OpenLayers.Projection("EPSG:4326");
var toProjection = new OpenLayers.Projection("EPSG:900913");
var position = new OpenLayers.LonLat(2.35,48.85)
.transform(fromProjection, toProjection);
var zoom = 12;
map.addLayer(mapnik);
map.setCenter(position, zoom );
map.render(element[0]);
}
};
});
将地图访问代码放在setTimeout中。您正在尝试立即访问地图。Angular 还没有完成地图渲染,所以setTimeout
是解决方案。
$timeout(function(){
map = new OpenLayers.Map("basicMap");
var mapnik = new OpenLayers.Layer.OSM();
var fromProjection = new OpenLayers.Projection("EPSG:4326"); // Transform from WGS 1984
var toProjection = new OpenLayers.Projection("EPSG:900913"); // to Spherical Mercator Projection
var position = new OpenLayers.LonLat(2.35,48.85).transform( fromProjection, toProjection); //Paris
var zoom = 12;
map.addLayer(mapnik);
map.setCenter(position, zoom );
},10);
相关文章:
- 使用angularjs中的rest调用通过id获取数据
- AngularJS-工厂中promise ID字段的返回值
- AngularJS-点击对象,访问它's ID&以该ID作为参数激发函数
- 如何获得家长's的父's的父's id in Angularjs
- AngularJS - 如何从嵌套的ng-repeat将id传递给api
- Angularjs:如何检查元素的子元素是否包含某些元素(通过ID)
- Angularjs ng-show == Id?
- 在模板angularJs中从控制器填充id属性的值
- 如何使用表行id作为angularjs函数的参数
- AngularJS ex.com/forum?id=1 to ex.com/#/forum?id=1
- AngularJS如何在用户发送表单时放置数据和id
- 将html ID与angularJS一起使用
- 用angularjs将id从一页传递到另一页
- AngularJS通过匹配ID访问更多详细信息
- 如何在 AngularJS 中通过 id 访问 iframe
- 在 ANGULARJS 中:s.Id as s.Name for s 在 Player 中显示 playerName i
- 使用 ID 更新文本区域值,而不是在 AngularJs 中使用 ng-model
- AngularJS:获取一个文本区域的值及其 ID 并将其分配给另一个 textare
- 带有指令不起作用的 AngularJS 动态表单字段 ID
- 包含的 HTML 模板中的 AngularJS id 属性