2016-09-22 77 views
1

我如何调用从我服务的方法在我的控制器,这样如何使用angularjs 1.5 ES6

import template from './tech.html' 
import styles from './styles.mcss' 
import TechService from './techService'; 

class Controller { 

    constructor(TechService) { 
    // css modules 
    TechService.consoleLog(); 

    this.styles = styles; 
    } 
} 

export const tech = { 
    template, 
    bindings: { 
    tech: '<' 
    }, 
    controller: Controller 
}; 

这难道不是作品我应该在哪里注入以及如何注入TechService

特定成分注入服务

该控制器是这样的模块

import angular from 'angular'; 

import {tech} from './tech'; 
import {techs} from './techs'; 
export const techsModule = 'techs'; 

angular 
    .module(techsModule, []) 
    .component('fountainTech', tech) 
    .component('fountainTechs', techs); 

和我的应用程序的根的一个组件是这样

import angular from 'angular'; 
import uiRouter from 'angular-ui-router'; 
import ngAnimate from 'angular-animate' 
import ngTouch from 'angular-touch' 
import uiBootstrap from 'angular-ui-bootstrap'; 

import {componentsModule} from './components'; 
import {commonModule} from './common'; 
import {techsModule} from './techs' 
import TechService from './techs/techService'; 

import routesConfig from './routes'; 

import './index.scss'; 


angular 
    .module('myApp', [ 
     // dependencies 
     uiRouter, ngAnimate, ngTouch, uiBootstrap, 

     // modules 
     componentsModule, 
     commonModule, 
     techsModule 
    ]) 
    .config(routesConfig); 

我想知道在哪里注入以及如何我的服务TechService打电话给我的方法consoleLog()

回答

1

最简单的解决方案是使用数组表示法:

export const tech = { 
    template, 
    bindings: { 
    tech: '<' 
    }, 
    controller: ['TechService', Controller] 
}; 
+0

如何angularjs知道该字符串( 'TechService')是否应该将TechService作为服务添加到模块TechModule中? –

+0

我看到'techsModule'已经是应用程序模块的一部分,所以它会没事的。 – dfsq