Reputation: 968
I know if i have a exported service,
import { Injectable } from '@angular/core';
@Injectable()
export class StudentService {
}
I can import this service into my component:
import { StudentService } from "./student-.service";
But, if my service is not a exported class, how do I import this unexported service into angular 2 component?
(function() {
"use strict"
class StudentService {
}
angular.module('test').service('StudentService');
})();
My component and service are not within the same folder/module.
My component is inside: AFolder/Student/student.component.ts
My service is inside: BFolder/Shared/student.service.js
AFolder and BFolder both are under MainFolder.
MainFolder
-AFolder
-BFolder
I cannot find the solution for my question both from Google and StackOverflow. Please advise. Thanks.
Upvotes: 1
Views: 337
Reputation: 222503
The reason why code is wrapped with IIFE like in the original post is that all variables and declarations stay local and don't leak to global scope. As a result, local variables cannot be retrieved from outside IIFE even when this is necessary.
AngularJS services are stored inside injector and can't be retrieved from the outside before application boostrap.
The only proper way to get StudentService
class from the outside is to export it. If this is module scope, IIFE is unneeded and undesirable here:
export class StudentService {...}
angular.module('test').service('StudentService', StudentService );
Upvotes: 1