我正在尝试使用 AngularJS 创建我的第一个应用程序。但是,如果我需要针对我的特定情况使用指令,我会感到有些困惑。
我有一个简单的地图页面,我需要在其中显示所选区域的纬度/经度值。目前我根本没有使用指令。我在控制器中做所有事情并使用部分来显示结果。我不打算在任何其他地方重复使用我的地图视图。这就是为什么我觉得我不需要指令。
另一方面,我在某处读到,每次您尝试在控制器中操作 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();
var southWest = bounds.getSouthWest();
$scope.southWestLat = southWest.lat();
$scope.southWestLon = southWest.lng();
});
});
/*
* Set zoom and other map attributes.
*/
google.maps.event.addListener($scope.map, 'some event', function() {
$scope.$apply(function () {
...
});
});
/*
* Add markers to map.
*/
google.maps.event.addListener($scope.map, 'another event', function() {
$scope.$apply(function () {
...
});
});
}
这是我的部分内容:
<div id="map-controllers" class="span4">
<form class="form-horizontal">
<div class="control-group">
<label class="control-label" for="inputNumber">Zoom:</label>
<div class="controls">
<input type="text" class="input-mini" placeholder="zoom" value="{{ zoom }}">
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputNumber">Latitude:</label>
<div class="controls">
<input type="text" class="input-large" placeholder="Latitude" value="{{ lat }}">
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputNumber">Longitude:</label>
<div class="controls">
<input type="text" class="input-large" placeholder="Longitude" value="{{ lon }}">
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputNumber">North East Latitude:</label>
<div class="controls">
<input type="text" class="input-large" placeholder="Latitude" value="{{ northEastLat }}">
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputNumber">North East Longitude:</label>
<div class="controls">
<input type="text" class="input-large" placeholder="Longitude" value="{{ northEastLon }}">
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputNumber">South West Latitude:</label>
<div class="controls">
<input type="text" class="input-large" placeholder="Latitude" value="{{ southWestLat }}">
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputNumber">South West Longitude:</label>
<div class="controls">
<input type="text" class="input-large" placeholder="Longitude" value="{{ southWestLon }}">
</div>
</div>
</form>
</div>