V
V
vasIvas2015-08-05 01:05:13
Angular
vasIvas, 2015-08-05 01:05:13

How to properly inject dependencies in es6?

I came close to AngularJS for the first time and I experience difficulties, for some reason, at every step.
I have custom-service.js in which is the following code -

export class CustomService {
  constructor(){

  }
}

There is a custom-directive.js directive -
export function customDirective(/*как мне тут получить CustomService?*/){
  return {
    ///...
  };
}

There is also app.js -
export const app = angular.module('app', []);
And there is main.js -
import {app} from 'app.js';
import {CustomService} from 'custom-service.js';
import {customDirective} from 'custom-directive.js';

app.service('customService', [CustomService]);
app.directive('customDirective', [customDirective]);

angular.bootstrap(document, [app]);
How to get 'customService' in customDirective?
And what else is wrong? And I would be very glad to receive instructions
on the topic of avoiding problems with minification using the example of CustomService.
And I apologize for the topics that are created with the speed of a machine gun. But I just really want to quickly understand these little things. I also try to google in English, but there are so many answers that choosing those that seem the most reasonable, but as a result turn out to be ineffective, the feeling that I lost my strength in vain. For three hours I can not find a solution ..

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2015-08-05
@vasIvas

// custom.directive.js

/** @ngInject */
export default function(customService){
  return {
    ///...
  };
}

// app.module.js
angular
    .module('app')
    .service('customService', require('./custom.service.js'))
    .directive('customDirective', require('./custom.directive.js'))
;

// bootstrap.js
import './app.module';

angular.bootstrap(document, [app]);

I usually do something like this.
Separately, about the ngInject annotation - it is needed so that ng-annotate can 100% figure out that we want to inject something there and automatically add the $inject property with a list of dependencies.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question