I'm doing a project using AngularJS 1.5.8 and OpenLayers3.
I'm able to display the map and set the various map components. However when I set a control on the map that has either ng-repeat or ng-if directives, nothing is displayed. When I test the code outside of the map (not as a control) it works fine.
I have already looked at the angular-openlayers-directive project but I think it has too little activity to depend on it for my project.
The JSFiddle link is here . And the same code below:
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ol3/3.18.2/ol.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/ol3/3.18.2/ol.css"></link>
<style>
.lister {
top: 5em;
left: 0.5em;
}
</style>
<div ng-app="myApp">
<div ng-controller="MapsController">
<div id="map" class="map"></div>
<div id="lister" class="lister ol-unselectable ol-control">
<div ng-repeat="item in items" ng-click="showMarkerPopup($index)">
{{item.name}}
</div>
</div>
<div id="other" class="other ol-unselectable ol-control">
Other Object
</div>
</div>
</div>
JS:
var app = angular.module('myApp', []);
app.controller('MapsController',['$scope','$window','$compile',function($scope,$window,$compile){
var lister = angular.element(document.querySelector('#lister'));
$compile(lister)($scope);
var listerControl = new $window.ol.control.Control({
element: $window.document.getElementById('lister')
});
$scope.items = [{
name: "ball",
color: "blue"
}, {
name: "tree",
color: "green"
}, {
name: "house",
color: "red"
}];
$window.map = new $window.ol.Map({
layers: [new $window.ol.layer.Tile({
source: new $window.ol.source.OSM()
})],
target: 'map',
view: new $window.ol.View({
center: window.ol.proj.transform([36.823219, -1.288811], 'EPSG:4326', 'EPSG:3857'),
zoom: 10
})
});
$window.map.addControl(listerControl);
}]);
The other control I have placed works fine. And any other that have angular bindings except the ng-repeat and ng-if directives.
Help me look at what I could be doing wrong.
I have managed to figure it out. I went with a custom directive as follows:
app.directive('compile', ['$compile', function ($compile) {
return function(scope, element, attrs) {
scope.$watch(
function(scope) {
return scope.$eval(attrs.compile);
},
function(value) {
element.html(value);
$compile(element.contents())(scope);
}
)};
}]);
And changed the control element to:
<div id="lister" class="lister ol-control">
<div style="overflow:auto; max-height: 400px;" compile="item_list" ></div>
</div>
The working JSFiddle is here
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.