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

时间:2017-03-17 11:15:16

标签: javascript angularjs service components directive

这是我的指令索引:

'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;

我需要从Menu指令访问Signup Service,但我有这个错误 - Incorrect injection token! Expected service name as string, got function factory()。如何以及将指令注入服务的最佳做法是什么。对于gelp而言。

1 个答案:

答案 0 :(得分:0)

您的依赖注入似乎是错误的。

而不是

Sub Test()

 Dim Sh1 As Worksheet, Sh2 As Worksheet, Sh3 As Worksheet
 Dim Sr As Long, Lr1 As Long, Lr2 As Long, Lr3 As Long
 Dim i As Long
 Dim Rng As Range, fRng As Range

 Set Sh1 = Sheets("sheet1")
 Set Sh2 = Sheets("sheet2")
 Set Sh3 = Sheets("sheet3")

 Sr = 2
 Lr1 = Sh1.Range("A" & Sh1.Rows.Count).End(xlUp).Row
 Lr2 = Sh2.Range("A" & Sh2.Rows.Count).End(xlUp).Row
 Lr3 = Sh3.Range("A" & Sh3.Rows.Count).End(xlUp).Row + 1
 Set Rng = Sh1.Range("A" & Sr & ":A" & Lr1)

 For i = Sr To Lr2
  Set fRng = Rng.Find(Sh2.Range("A" & i).Value, , xlValues, xlWhole, xlByRows, xlNext)
  If Not fRng Is Nothing Then
   Sh3.Range("A" & Lr3 & ":B" & Lr3).Value = Sh1.Range("A" & fRng.Row & ":B" & fRng.Row).Value
   Lr3 = Lr3 + 1
  End If
 Next

End Sub

应该有

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