简体   繁体   English

如何使用ES6将服务注入AngularJS中的指令

[英]How to inject service into directive in AngularJS with ES6

Here are my directives index: 这是我的指令索引:

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

and directive: 和指令:

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;

I need to get access to Signup Service from Menu directive, but i have this error - Incorrect injection token! Expected service name as string, got function factory() 我需要从Menu指令访问Signup Service ,但出现此错误- Incorrect injection token! Expected service name as string, got function factory() Incorrect injection token! Expected service name as string, got function factory() . Incorrect injection token! Expected service name as string, got function factory() How and what is the best practice to inject service to directive. 如何以及将指令注入服务的最佳实践是什么。 Thx for gelp. Thx的凝胶。

Your dependency injection seems to be wrong. 您的依赖项注入似乎是错误的。

Instead of 代替

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

there should be 应该有

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM