0

I have create a simple interface

module Modules.Part
{ 
     export interface IPart 
     {
         PartId: number;
         partNumber: string;
         description: string;
     }
}

then i have declare this interface in another interface

module Interfaces.Scopes {
    export interface IPartScope extends ng.IScope {
        part: Modules.Part.IPart;
        vm: Controllers.PartCtrl;
    }
}

i have use this interface in my class

module Controllers
{
    export class PartCtrl
    {


        constructor(public scope:Interfaces.Scopes.IPartScope)
        {
            scope.part.PartId = 1;
            scope.part.partNumber = "123part";
            scope.part.description = "description";           

        }

    }
}

when i am going to set property of IPart interface in my class it's give me following error

TypeError: Cannot set property 'PartId' of undefined 

please let me know how to solve this

Ryan Cavanaugh
  • 209,514
  • 56
  • 272
  • 235
Shivkumar
  • 1,903
  • 5
  • 21
  • 32
  • This is the error you get in JavaScript when you try to set a property on a value that is undefined. The code you have here is not enough to determine where that undefined value came from. – Ryan Cavanaugh Dec 20 '13 at 07:39

2 Answers2

0

I'd say, you first need to initialize the part property of the PartCtrl.scope property (which you implicitly defining through the constructor):

constructor(public scope:Interfaces.Scopes.IPartScope)
{
  scope.part = [initializer];

  scope.part.PartId = 1;
  scope.part.partNumber = "123part";
  scope.part.description = "description";           
}

Since the scope.part is of interface type IPart, you can't create an instance of it but rather have to resolve it somehow. For example:

export interface IActivator<T> {
  new (): T;
}

export class PartCtrl<TPart extends IPart>
{
  constructor(public scope:Interfaces.Scopes.IPartScope, 
              activator: IActivator<TPart>)
  {
    scope.part = new activator();

    // ...
  }
}

Now you can use the PartCtrl<TPart> in the following way:

export class ConcretePart implements IPart
{
  public PartId: number;
  public partNumber: string;
  public description: string;
}

var ctrl = new PartCtrl(ConcretePart);

Hope this helps.

volpav
  • 5,090
  • 19
  • 27
0

You can initialize the property "part" with an anonymous object.

scope.part =
{
   PartId = 1;
   partNumber = "123part";
   description = "description"; 
}

Of course you can also define a class that implements IPart and instantiate it.

Farshid Saberi
  • 917
  • 13
  • 15