Reusable es6 class in ionic2

Simple example: From my server, I get an array of friends. I want to create a ‘Friend’ class with attributes like ‘name’ and methods like ‘sendMessage(msg)’. How do I accomplish this using es6 classes?

I have tried creating a simple injectable class, but this gives me the error: “Uncaught TypeError: Cannot read property ‘getOptional’ of undefined.” The error goes away if I change the constructor to constructor(){… but that defeats the purpose.

import {Injectable,Injector} from 'angular2/core';

export class Friend{
  constructor(name){ = name

    console.log("My name is " +;

How can I use the constructor in a provider / other class? I’ve tried injecting it like a normal data provider and including it in the app.js providers array etc, but I can’t get it to work like I expect.

How is the injector to know what to pass for “name” when injecting? This doesn’t seem like something you want to be using DI for. Can you just import Friend from the service that retrieves friends, and have a method in that service that composes and returns an array of them? That method will know what each Friend's name is.

1 Like

Sure, thats what I was doing in my Angular 1 app.

Now that es6 is around, I assume there is a better way?

1 Like

in TypeScript you can use interfaces…

Do you want use this model like service? I don’t think, so you no need provider. After importing, you can create instance of class.