2017-03-17 89 views
0

這裏是注入服務到指令在AngularJS我的指示指標:如何使用ES6

'use strict'; 

import SignupService from './core/services/signup.service'; 

import FooterDirective from './components/footer/footer.directive'; 
import MenuDirective from './components/menu/menu.directive'; 

export default angular.module('index.components', []) 
    .directive('footer', FooterDirective) 
    .directive('menu', [MenuDirective, function(SignupService) {}]); 

和指令:

import tmpl from './menu.tpl.html'; 

class Menu { 
    constructor($scope) { 
     this.scope = $scope; 
     this.restrict = 'AC'; 
     this.transclude = true; 
     this.replace = true; 
     this.templateUrl = tmpl; 
     this.scope = { 
      ngSrc: "@", 
     }; 
    } 

    link(scope, elem, attrs) { 

     console.log(scope.SignupService); 

     scope.logoutCall =() => { 
      this.SignupService.logoutSubmit(); 
     } 
    } 
} 
function factory() { 
    "ngInject"; 

    return new Menu(...arguments); 
} 

export default factory; 

我需要從菜單指令訪問Signup Service,但我有這個錯誤 - Incorrect injection token! Expected service name as string, got function factory()。如何以及如何將服務注入指令的最佳做法。 Thx for gelp。

回答

0

您的依賴注入似乎是錯誤的。

而不是

.directive('menu', [MenuDirective, function(SignupService) {}]); 

應該有

.directive('menu', ['SignupService', function(SignupService) {}]); 
+0

又在哪兒聲明菜單指令? – Lukas

+0

然後看看這個。這似乎是類似於您的問題 http://stackoverflow.com/questions/40333189/angularjs-1-5-inject-service-into-directive-in-es6 –

+0

我試過了,我有undefined :( – Lukas