I have a service that require external library, for example moment. I import that library as ES6 module. But because AngularJS service defined using closure approach, library is not available when funcA() is being been called.
service.js
import moment from 'moment';
export function DataService() {
return {
funcA: function() {
return moment().startOf('day').fromNow();
}
};
}
controller.js
MyCtrl.$inject = ['$scope', '$state', 'DataService', ...];
export function MyCtrl($scope, $state, DataService, ...) {
function setInitialDate () {
ctrl.someDate = DataService.funcA();
}
}
app.js
import {DataService} from './service.js';
import {MyCtrl} from './controller.js';
var services = angular.module('services', []);
services.factory('DataService', DataService);
angular.module('mycontrollers').controller('MyCtrl', MyCtrl);
I can import external library into window object, and get access to it inside service using $window. Is there exists other way to get access to library, that was imported as ES6 module, from inside AngularJS service?
funcA(). It is not a message in console, it is the state ofmomentvariable when function is being executed.