khalil _diouri khalil _diouri - 2 months ago 15
AngularJS Question

How inject a service using angularjs 1.5 es6

I how to call a method from my service in my controller for a specific component like this

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


this it not works where should I inject and how to inject TechService

this controller is a component of this module

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


and the root of my app is this

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


I want to know where to inject and how my service TechService to call my method consoleLog()

Answer

The simplest solution is to use array notation:

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