简体   繁体   English

Typescript类作为Angular中的成员变量?

[英]Typescript Class as a Member Variable in Angular?

I'm learning Angular and TypeScript and I have a question or two. 我正在学习Angular和TypeScript,但有一个或两个问题。

With TypeScript can you have one class/object be the variable type for an other object as shown below? 使用TypeScript,您可以让一个类/对象成为另一对象的变量类型,如下所示吗? If so how do you reference the all of the members using Angular. 如果是这样,您如何使用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: 在Angular模板中,我将需要使用以下内容:

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

How can I achieve this? 我该如何实现?

Classes define types. 类定义类型。 Those types can be used wherever any of the default types can be used. 只要可以使用任何默认类型,就可以使用这些类型。

In short yes. 简而言之,是的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 以角度2类存储成员变量的状态 - store state of member variable in angular 2 class 类成员变量是动态的。 取决于打字稿中地图的键 - Class member variable are dynamic. Depending upon the keys of a map in typescript 打字稿在构造函数内部和外部声明类成员变量 - Typescript declaring class member variable inside and outside of constructor 将Javascript成员变量转换为Typescript成员变量 - Converting Javascript member variable to Typescript member variable Angular + Typescript:在控制器类中使用指令类作用域变量 - Angular + Typescript: Use Directive Class Scope Variable in Controller Class 如何在 Type Script Angular 中为类的成员变量赋值 - How to assign value to the member variable of a Class in Type Script Angular 类在“键入”变量(VS接口)和“建模”(用于Typescript Angular 2项目)中的用法 - Usage of class in 'typing' the variable (vs interface) and 'modelling' (for Typescript Angular 2 project) 如何在TypeScript中将成员变量声明为扩展类型? - how to declare member variable as extended type in TypeScript? TypeScript 中 c# 类虚拟成员的等价物 - Equivalent of c# class virtual member in TypeScript Typescript类成员未定义-编译器流程正确吗? - Typescript class member undefined - Compiler Flow correct?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM