2016-09-22 107 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