Note that there are some explanatory texts on larger screens.

plurals
  1. POService still available from outside the controller AngularJs
    text
    copied!<p>I've build a service for provide the posibility to add markers to google maps intance, but this only happens when I'm inside a particular controller using the ngRoute service. The issue is that when I first load the app my service doesn't work outside the controller where it is injected, when I switch to the controller where it is injected and it works, but when I go backward to the controller where it isn't injected my service works! Where is the problem here?</p> <p><strong>ngRoute config</strong></p> <pre><code>var climbingApp = angular.module('climbingApp', ['ngRoute'], function($routeProvider){ $routeProvider.when('/', { controller: 'Main' }); $routeProvider.when ('/newSector', { templateUrl: 'app/templates/newSector.html', controller: 'addSector' }); }); </code></pre> <p><strong>Controllers</strong></p> <pre><code> function addSector($scope, Markers) { Markers.addListener( function ( e ){ $scope.lat = e.latLng.lat(); $scope.lng = e.latLng.lng(); }); } function Main ($scope) { $scope.initLocation = { lat: -54.798112, lng: -68.303375 }; } </code></pre> <p><strong>Service</strong></p> <pre><code>climbingApp.factory('Markers', function($rootScope){ return { addListener: function( callback ) { google.maps.event.addListener( map, 'click', function( e ) { var marker = new google.maps.Marker({ position: e.latLng, map: map }); $rootScope.$apply( function(){ callback( e ); }); } ); } } }); </code></pre> <p>I guess the problem is that the listener is still atached to the map instance, but how could I destroy it?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload