2017-03-09 67 views
-4

如何创建名为Bill的类,该类具有简单属性,如string and number &另一个属性是另一个类CustomerObject对象中的角2对象

我想比尔json对象bill: Bill这个样子的:

bill = { 

    id: "", 
    usage : "", 
    total : "0", 

    customer : { 
     customerFirstname :"", 
     customerLastname :"", 
    } 

    } 

这个对象是AngularJS正确的,但不是在Angular2

Customer类:

export class Customer{ 
    customerFirstname: string; 
    customerLastname: string; 
    constructor(){ 
    } 
} 

和比尔类:

import {Customer} from '../customer/customer.model'; 
export class Bill { 

    id : string ; 
    usage: string; 
    total: number; 
    customer : Customer; 
    constructor(){ 
    } 
} 

回答

1

您可以使用extends

编号:https://www.typescriptlang.org/docs/handbook/classes.html

对于示例:

export class Customer { 

    public customerFirstname: string; 
    customerLastname: string; 
    constructor() { 
    } 
    } 

export class Bill{ 

    id: string; 
    usage: string; 
    total: number; 
    customer: Customer; 
    constructor() { 
    } 
} 

使用:

import { Component, OnInit } from '@angular/core'; 
import { Bill } from '../models/Animal'; 

@Component({ 
    moduleId: module.id, 
    selector: 'home', 
    templateUrl: 'home.component.html' 
}) 
export class HomeComponent implements OnInit { 
    bill: Bill = new Bill(); 

    constructor() 
    { 
     console.log(this.bill.customer.customerFirstname) 
    } 

    ngOnInit() { } 
} 
+0

我已经延长,但通过显示'{{bill.customer.customerFirstname}}' – Manu

+0

'{{bill.customerFirstname}}''''''''''''''你只需要做到这一点就不会在'html'中工作。 @Manu – User1911

+0

@Manu更新了我的示例。 – User1911

0

我会做一些这样的台词:

export interface ICustomerProperties { 
    customerFirstname: string; 
    customerLastname: string; 
} 

export class Customer { 
    customerFirstname: string; 
    customerLastname: string; 

    constructor (properties: ICustomerProperties) { 
     this.customerFirstname = properties.customerFirstname; 
     this.customerLastname = properties.customerLastname; 
    } 
} 

export interface IBillProperties { 
    id: string; 
    usage: string; 
    total: string; 
    customer: Customer; 
} 

export class Bill { 
    id: string; 
    usage: string; 
    total: string; 
    customer: Customer; 

    constructor (properties: IBillProperties) { 
     this.id = properties.id; 
     this.usage = properties.usage; 
     this.total = properties.total; 
     this.customer = properties.customer; 
    } 
} 

let customer = new Customer({ customerFirstname: 'first', customerLastname: 'last' }); 
let bill = new Bill({ id: '1', usage: '', total: '', customer: customer}); 
+0

@ArgOn我有扩展,但它不工作在HTML显示'{{bill.customer.customerFirstname}}',我得到的错误:'customerFirsname不存在对象帐单' – Manu

+0

如何实例化你的'对象' ? – Arg0n

+0

'bill = new Bill()' – Manu

1

其实完全没有必要在这种情况下类。只要使用界面,你会得到他们想要的行为,所以你的接口:

export interface Bill { 
    id : string ; 
    usage: string; 
    total: number; 
    customer : Customer; 
} 

export interface Customer{ 
    customerFirstname: string; 
    customerLastname: string; 
} 

您可以实例化一个新的Bill,像这样:

bill: Bill = <Bill>{}; 

,如果当你想输入你的数据,是JSON,

{ 
    "id":"1", 
    "usage":"usage", 
    "total":"0", 
    "customer":{ 
    "customerFirstname":"firstname", 
    "customerLastname":"lastname" 
    } 
} 

TS:

this.myService.getBill() 
    .subscribe(data => { 
    this.bill = data; 
    }) 
0

这是因为attribut customer尚未在Class Bill 实例化而所有变量都被声明被实例化

Customer Class: 

export class Customer{ 
    customerFirstname: string = ''; 
    customerLastname: string = ''; 
    constructor(){ 
} 
} 

比尔类:

import {Customer} from '../customer/customer.model'; 
export class Bill { 

    id : string = '' ; 
    usage: string = ''; 
    total: number = 0; 
    customer = new Customer; 
    constructor(){ 
    } 
}