AngularJS如何预加载控制器

时间:2015-06-16 05:14:30

标签: angularjs

我正试图通过'$ broadcast'访问我的谷歌地图(MapCtrl)上的一个功能。除非用户先加载了MapCtrl,否则无效。如何欺骗MapCtrl在后台加载以便它能听到我的广播?

CouponCtrl中的

(进行重要点击)

$rootScope.$broadcast(CONST.MAP_FIND_LOCATION, id);

$state.go('tabs.map');
MapCtrl中的

$rootScope.$on(CONST.MAP_FIND_LOCATION, function(id) {
  var slid = id;
  var smap = $scope.map;

  try{
    // var latlng = userService.locationManager.getLocationById(slid).getLatLng();
    // smap.panTo(latlng);

    var loc = userService.locationManager.getLocationById(slid);
    var address = loc.getFullAddress();

    // $scope.map.setZoom(11); directions set's the zoom automatically
    $scope.directionsDisplay.setDirections({routes:[]}); // resent display
    $scope.directionsDisplay.setMap($scope.map);

    var request = {
      origin: new google.maps.LatLng(
        userService.user.gps.coords.latitude,
        userService.user.gps.coords.longitude
      ),
      destination: address,
      travelMode: google.maps.DirectionsTravelMode.DRIVING
    }

    $scope.directionsService.route(request, function(response, status) {
     if (status == google.maps.DirectionsStatus.OK) {
       $scope.directionsDisplay.setDirections(response);
     }
    });

  } catch(e) {
    console.warn(e);
  }
});

0 个答案:

没有答案