我正在尝试编写一个指令来创建一个地图组件,这样我就可以编写如下内容:
<map></map>
现在指令看起来像这样:
angular.module('myApp')
.directive('map', function (GoogleMaps) {
return {
restrict: 'E',
link: function(scope, element, attrs) {
scope.$watch('selectedCenter', function() {
renderMap(scope.selectedCenter.location.latitude, scope.selectedCenter.location.longitude, attrs.zoom?parseInt(attrs.zoom):17);
});
function renderMap(latitude, longitude, zoom){
GoogleMaps.setCenter(latitude, longitude);
GoogleMaps.setZoom(zoom);
GoogleMaps.render(element[0]);
}
}
};
});
问题是指令中的“watch”在组件的可重用性方面看起来并不是很好。所以我想最好的事情是能够做这样的事情:
<map ng-model="selectedCenter.location"></map>
但是我不知道在自定义指令中使用角度指令是否是一件好事,或者我怎样才能在自定义指令的链接函数中获取 ng-model 属性中指示的对象。