Ant
Ant

Reputation: 1852

Access a service from inside a directive's compile

I am having a hard time to access a service inside a directive. I define my service via $http and $q, and inject it into my directive. But I can;t get the directive to access the service.

service.js

'use strict';
var app = angular.module('App.services', []);

app.factory('Classification', function($http,$q) {
    return {
        query: function getAll() {
            var deferred = $q.defer();
            $http.get('index.php/classifications').then(function(classi) { 
                deferred.resolve(classi.data);
            }, function getWebsitesError(reason) {
                deferred.reject(reason);
            });
            return deferred.promise;
        }
    };
});

app.js

'use strict';

/* App Module */
var app = angular.module('App', ['App.controllers', 'App.services', 'App.directives',  'ui']);

app.config(['$routeProvider', function($routeProvider) {
  $routeProvider.
      when('/', {templateUrl: 'partials/welcome.html'}).
      when('/websites/:websiteId', {templateUrl: 'partials/website/details.html',     controller: 'WebsiteDetailsCtrl'}).
      otherwise({redirectTo: '/'});
}]);

and my directive.js :

'use strict';
var app = angular.module('App.directives', ['App.services']);

app.directive("regionselect",['Classification', '$compile', function($compile, Classification){
    Classification.query();<-- Throw an exception : has no method query()
    return{
        restrict : "E",
        templateUrl : "/js/directives/locationSelect3.html",
        transclude: true,
        compile: function (tElement, tAttr, transclude){
            var loaded = false;                
        }
    };
}]);

Any idea what I am doing wrong?

Upvotes: 2

Views: 1603

Answers (1)

Michelle Tilley
Michelle Tilley

Reputation: 159115

Could it be the ordering?

app.directive("regionselect",['Classification', '$compile', function($compile, Classification){

You declare

['Classification', '$compile',

but the the function says

function($compile, Classification){

Which is backwards.

Upvotes: 3

Related Questions