angular - too much glue in angular2 DI or there is another way? -
in angular 1.x services injected specifying registered name. example using typescript:
// appointments.ts export interface iappointmentservices { retrieve(start: date, end: date): appointment[] } // appointment-services.ts: class appointmentservices implements iappointmentservices { retrieve(start: date, end: date): appointment[] { ... } } angular.module('appointments') .service('appointmentservices', appointmentservices); // appointment-controller.ts class appointmentcontroller { constructor (private service: iappointmentservice) { } // pay attention 'appointmentservices' registered name // not hard reference class appointmentservices static $inject = ['appointmentservices'] }
pay attention controller implementation has no reference in way file nor class implements service
but in angular 2, accomplish similar di have in these lines:
import {component} 'angular2/core'; import {appointmentservices} 'appointment-services'; @component({ ... providers: [appointmentservices] }) export class appointment { constructor (service: appointmentservices) { ... } }
pay attention in second import above, have specify location appointmentservices implemented name of class represent glue between component , implementation of service
is there way in angular2 inject service without specifying class , file implements it?
i think angular2 approach kind of step in terms of ioc accomplished in predecessor if di has done in such way!
there no such way, because how import / export
functionality works. in order import
something, must equally exported
(by same name), somewhere else.
hence why in 1 file (appointment-services) have
export class appointmentservices
and other component file use object destruction "grab" specific thing (which exported it)
import { appointmentservices } 'appointment-services';
these 2 linked, in that, how access each other. although may seem "backwards", typescript , ides have power refactor entire library ease now, since aware of these export/imports.
so if wanted change export class anotherappoinmentservice
instead, refactor it, , ide automatically switch them you!
note: angular1 modules stored "string", why things loosely specified. typically had myclass.name example
Comments
Post a Comment