I want to create an angular map directive for openlayers map application. For example this is an example of map.
I created an angularjs directive.
(function () {
"use strict";
angular.module("my.map").directive("map", [mapDirective]);
function mapDirective() {
return {
"template": "<div id='map' style='width: 400px; height: 300px'></div>",
"link": function (scope) {
var map = new ol.Map({
view: new ol.View({
center: [0, 0],
zoom: 1
}),
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
})
],
target: "map"
});
}
};
}
})();
This sample works fine. But I hard coded the map element id name. And I want to get id
value from scope.
(function () {
"use strict";
angular.module("my.map").directive("map", [mapDirective]);
function mapDirective() {
return {
"template": "<div id='{{target}}' style='width: 400px; height: 300px'></div>",
"scope": {
"target": "@"
},
"link": function (scope) {
var target = scope.target ? scope.target: "map";
var map = new ol.Map({
view: new ol.View({
center: [0, 0],
zoom: 1
}),
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
})
],
target: target
});
}
};
}
})();
But this does not show the map.
Openlayers map target property accepts 3 types: Element | string | undefined.
Sou you can set the target as element[0]
But you set directive parameter replace:true
, so map changes with directive.
(function () {
"use strict";
angular.module("my.map").directive("map", [mapDirective]);
function mapDirective() {
return {
"template": "<div style='width: 400px; height: 300px'></div>",
"replace": true,
"scope": {
},
"link": function (scope) {
var target = scope.target ? scope.target: "map";
var map = new ol.Map({
view: new ol.View({
center: [0, 0],
zoom: 1
}),
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
})
],
target: element[0]
});
}
};
}
})();