I have a class like this
import {Injectable } from '@angular/core';
@Injectable()
export class Address {
street: string;
... some other attributes
}
And a class like this
import {Injectable } from '@angular/core';
import { Address } from './address';
@Injectable()
export class Company {
name: string;
constructor(public address: Address) {}
}
And I use the above classes like this
import { Injectable } from '@angular/core';
import { Address } from './address';
import { Company } from './company';
@Injectable()
export class Employee {
name: string;
lastName: string;
constructor(public address: Address,
public company: Company) {}
}
Now, I use the Employee class in a component like this
import {Component } from '@angular/core';
import { Employee } from '../model/employee';
@Component({
moduleId: module.id,
selector: 'my-employee',
templateUrl: '../views/employee.html
})
export class EmployeeComponent {
constructor(public employee: Employee){}
... some other stuff;
}
My questions are:
1.- Why I'm getting the same Address object for both the company and the employee address objects? 2.- How can I get different instances of Address object?
3.- I know, I can make a new instance of Address in the constructor of the EmployeeComponent, with the new operator, and then assign it to the let's say employee.address, but then, what is the purpose of DI in Angular 2?