从指令的编译中访问服务

时间:2013-04-20 03:06:47

标签: angularjs angularjs-directive

我很难在指令中访问服务。我通过$ http和$ q定义我的服务,并将其注入我的指令。但是我可以获得访问该服务的指令。

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: '/'});
}]);

和我的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;                
        }
    };
}]);

知道我做错了吗?

1 个答案:

答案 0 :(得分:3)

可能是订购吗?

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

你宣布

['Classification', '$compile',

但功能说

function($compile, Classification){

哪个是倒退。