Map not loaded when wrapped inside another directive (where the map object is defined)
I have a directive in my app that wraps the google-maps directive and it works fine if I define the map object in the controller. But when I define the map object inside the link function, it doesn't seem to be getting the map object. When looking inside the google-map's scope (using angular.element("google-map >").scope() ), it shows center to be defined so I am unsure why the map isn't loaded.
var app = angular.module('myApp',[
'google-maps',
])
app.directive('myDir', function($http) {
return {
restrict: 'AE',
template: '<google-map center="map.center" zoom="13" draggable="true" pan= "1" control="map.control"></google-map>',
link: function(scope, elem, attrs) {
scope.map = {
center: {
latitude: 40.35,
longitude: -74.6702
},
control:{}
}
},
};
});
My guess is your having a template conflict since your conflicting with out map. Change your template to this,
template: '<my-google-map center="map.center" zoom="13" draggable="true" pan= "1" control="map.control"></my-google-map>',
If its working when initialized in a controller, why not initialize the map object in a controller?
var app = angular.module('myApp',[
'google-maps',
])
app.directive('myDir', function($http) {
return {
restrict: 'AE',
template: '<google-map center="map.center" zoom="13" draggable="true" pan= "1" control="map.control"></google-map>',
controller: function($scope) {
$scope.map = {
center: {
latitude: 40.35,
longitude: -74.6702
},
control:{}
}
},
};
});
But this sounds like a priority issue. I.e. the google-map directive is initialized before your directive. I.e. the map object is not put into the scope until after the google-map directive was initialized.
@nmccready I tried doing that but still won't load the map. I am trying to get a functionality where
@rickhuizinga The reason why I would want the map object to be initialized in the directive is that I would be taking zip code through the defined my-dir directive which then creates a center object. This is the reason I wrapped google-map directive in my own defined directive. I tried adding a higher priority for my-dir but still it won't load the map
@sanke93 I didn't mean to question why you wanted to put it in a directive, just suggesting that your directive implement a controller rather than link function.
@rickhuizinga Okay! I misunderstood that, but even after trying to define the map object in directive's controller, the map isn't being rendered. So is there any other way I can define the map object inside a directive and not a controller?
I think the problem is that your custom directive's scope is not being initialized prior to the loading of its template.
I don't have time to research this for you, but I'll leave this issue open in case anyone else is trying to do the same or has a suggestion.
I ran into a problem similar to this. What worked for me was to define the map object in a prelink function. Try something like this, let me know if it works for ya.
var app = angular.module('myApp',['google-maps']);
app.directive('myDir', function($http) {
return {
restrict: 'AE',
template: '<google-map center="map.center" zoom="13" draggable="true" pan= "1" control="map.control"></google-map>',
link: {
pre: function(scope, elem, attrs) {
scope.map = {
center: {
latitude: 40.35,
longitude: -74.6702
},
control:{}
}
},
post: function(scope,elem,attrs){
//do normal link stuff here
}
}
};
}
@sanke93 If this suggestion works for you, please acknowledge and close the issue. Thanks.
FYI more info here: https://github.com/angular/angular.js/wiki/Understanding-Directives
The difference is that PreLinkingFunction() will fire on the parent first, then child, and so on. A PostLinkingFunction() goes in reverse, firing on the child first, then parent, and so on.
So the problem with your original code is that linking is happening on the google map directive before it runs on your directive, so your scope's map object wasn't available when the google map directive was linked. At least, this is my understanding of the issue.
@sharpone74 Thanks for finding and sharing this. It looks like the answer.
Yup this worked! Thank you!
:+1: