我正在尝试使用AngularJS创建我的第一个应用程序.但是,如果我需要为我的特定情况使用指令,我有点困惑.
我有一个简单的Map页面,我需要显示所选区域的lat/lon值.目前我根本没有使用指令.我在控制器中执行所有操作并使用partials来显示结果.我不打算在任何其他地方重复使用我的地图视图.这就是为什么我觉得我不需要指令.
另一方面,我在某处读到,每当你试图操纵控制器中的DOM时(我正在使用google maps API),你应该将该部分移动到指令中.
这是我的简单控制器:
function MapViewController($scope) {
$scope.zoom = 6;
$scope.lat = 37;
$scope.lon = -122;
var mapOptions = {
center: new google.maps.LatLng($scope.lat, $scope.lon),
zoom: $scope.zoom,
mapTypeId: google.maps.MapTypeId.HYBRID
};
$scope.map = new google.maps.Map(
document.getElementById('map-canvas'), mapOptions);
/*
* Update zoom and other map attributes.
*/
google.maps.event.addListener($scope.map, 'center_changed', function() {
$scope.$apply(function () {
$scope.zoom = $scope.map.getZoom();
var center = $scope.map.getCenter();
$scope.lat = center.lat();
$scope.lon = center.lng();
var bounds = $scope.map.getBounds();
var northEast = bounds.getNorthEast();
$scope.northEastLat = northEast.lat();
$scope.northEastLon = northEast.lng(); …Run Code Online (Sandbox Code Playgroud)