Services do not fire in directives when on device


#1

Hey guys,

I am new to Ionic and am having an issue getting a service to fire when called inside a directive. The funny thing is that it works fine using ionic serve in the browser but does not fire when either emulating or when it is loaded on device. There are no errors to debug when it does not get called.

.directive('gmap', function(CarService) {

    var createMap = function () {
        CarService.getPosition();
    };

    return {
        restrict: 'E',
        link: createMap
    }
});

.service('CarService', function() {
    this.getPosition = function() {
        alert('this is a alert');
    }
});

One thing to note is that when calling the service from a ng-click inside the controller it does work in emulation and on the device.

.controller('MapCtrl', function(CarService) {

    var self = this;

    self.centerMap = function() {
        CarService.getPosition();
    }
});

Any help would be greatly appreciated. I am really enjoying the framework so far.


#2

Injecting services into directives is pretty straight forward. But you should not be coupling view and model. Bad practice.

Seems to be working here : http://plnkr.co/edit/M8omDEjvPvBtrBHM84Am?p=preview