2017-03-09 66 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(){ 
    } 
}