1

I'm learning Angular and TypeScript and I have a question or two.

With TypeScript can you have one class/object be the variable type for an other object as shown below? If so how do you reference the all of the members using Angular.

/* begin cmsfiles.ts */

export class CMSFiles {
  fileID: number;
  fileDateTime: Date;
  fileName: string;
  description: string;
}
/* end cmsfiles.ts */

/* begin news.ts */

import { CMSFiles } from './cmsfiles';

export class News {
  newsID: number;
  postDateTime: Date;
  title: string;
  body: string;
  file1: CMSFiles;
  file2: CMSFiles;
}
/* end news.ts */

In the Angular template I will need to use something like this:

<input [(ngModel)]="news.file1.Title" placeholder="title" />

How can I achieve this?

2
  • The field Title doesn't exist on CMSFiles. Did you mean fileName? Commented Jan 11, 2017 at 19:37
  • Yes, you are correct I do mean fileName and not title. Commented Jan 11, 2017 at 20:17

1 Answer 1

1

Classes define types. Those types can be used wherever any of the default types can be used.

In short yes.

Sign up to request clarification or add additional context in comments.

2 Comments

do you know if <input [(ngModel)]="news.file1.fileName" placeholder="fileName" /> is correct syntax?
if the properties of the CMSFiles class are public yes. :) It's just normal js notation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.