Closed aabm00 closed 7 years ago
You need to import the name of the exported Angular module and add it to your dependency list:
import d3service from '.../d3.service';
export class TestComponent {
message;
d3;
/*@ngInject*/
constructor(d3) {
this.message = 'Hello';
this.d3 = d3;
}
}
export default angular.module('MyApp', [d3service]) // <--- here
.config(routes)
.component('test', {
template: require('./test.html'),
controller: TestComponent,
controllerAs: 'testCtrl'
})
.name;
Since the d3service
imported would be the name of the Angular module exported here:
export default angular.module('TGMApp.d3', [])
.factory('d3', d3Service)
.name;
Thanks for you answer.
Making your changes It works!!!!, but trying things just before I saw your answer I found another way that it works too, and it's more inline with the version 3.7.6 of the generator, but now I would like to know if my version have some negatives implications that I don't know. So I am going to expose my version, exposing only the differences.
d3.service.ts
When creating a factory service with the generator it creates the default export of the module but also and export of the Service (in this case the d3Service), so I delete this export
'use strict';
const angular = require('angular');
const d3 = require('d3');
/*@ngInject*/
function d3Service() {
// Service logic
// ...
/* We could declare locals or other D3.js
specific configurations here. */
return d3;
}
export default angular.module('TGMApp.d3', [])
.factory('d3', d3Service)
.name;
app.ts
I have added the services (moment, Rx and d3) as dependency in the module. With your version isn't necessary
'use strict';
const angular = require('angular');
// import ngAnimate from 'angular-animate';
const ngCookies = require('angular-cookies');
// ADDED
import 'bootstrap/dist/js/bootstrap.min.js';
import 'angular-bootstrap-datetimepicker';
....
import '../components/d3/d3.service';
import '../components/moment/moment.service';
import '../components/rxjs/rxjs.service';
import test from './test/test.component';
// ADDED- END
angular.module('TGMApp', [
ngCookies,
....
moment,
Rx,
d3,
test
test.component
Because I didn't export the d3Service function in d3.service.ts I dont have to import it here and either add it to the dependency list of the exported Angular module
'use strict';
const angular = require('angular');
const uiRouter = require('angular-ui-router');
import routes from './test.routes';
export class TestComponent {
message;
d3;
/*@ngInject*/
constructor(d3) {
this.message = 'Hello';
this.d3 = d3;
};
}
export default angular.module('TGMApp.test', [uiRouter])
.config(routes)
.component('test', {
template: require('./test.html'),
controller: TestComponent,
controllerAs: 'testCtrl'
})
.name;
In this way it's easiest but now I am wondering if I am missing something and in this new version of the generator it's necessary to export the Service (in this case the d3Service) for some reason I don't know.
Thanks
@aabm00 why do you have to make a service? I just want to use moment in a controller... Should be relatively simple:
npm install moment --save
Add moment to webpack.make.js:
config.entry = {
app: './client/app/app.js',
polyfills: './client/polyfills.js',
vendor: [
...
'moment'
Inject moment into app.js
angular.module('myApp', [... require('moment')])
'use strict'; const angular = require('angular');
const uiRouter = require('angular-ui-router'); const moment = require('moment');
import routes from './dashboard.routes';
export class DashboardComponent { current_time = moment().format();
/@ngInject/ constructor() { this.message = 'Hello'; } }
export default angular.module('myApp.dashboard', [uiRouter, moment]) .config(routes) .component('dashboard', { template: require('./dashboard.html'), controller: DashboardComponent }) .name;
Results in the error:
TypeError: moment is not a function
What am I missing?
@tharrington moment is not an angular library, so you can't add it to Angular's dependency injector. All you need is this at the top of your file:
const moment = require('moment');
or
import moment from 'moment';
Hi @tharrington.
As @Awk34 comment moment isn't an angular library so to add it to Angular's dependency you need to make a service with the it as explained @Awk34 in this post.
Yes, but it isn't necessary to include it as an Angular dependency.
socket.io
Hi
I am trying to load the libraries d3.js, moment.js and Rx.js as config.entry -->vendor in the webpack.make.js and once loaded create a service with every one, but I have tried everything and nothing. I am totally stuck. I'd appreciate so much any help.
The steps i have followed are:
In webpack.make.js
I create a service for every library, for example for d3.js is
d3.service.ts
Then in the app.ts I import the 3 services
app.ts
Then I want in the test component to use the created services, in this case for example the d3 service
test.component
But I get the next error
I have tried to load them with the plugin webpack.ProvidePlugin as we did with jQuery see this post but neither.
Thanks !!!