Class at Angular 2

Asked

Viewed 320 times

0

This is my class at 2

export class LinhaModel {
    _id: string;

    Cidade: {
        id: string;
        CidadeNome: string;
    }

    Operador: {
        id: string,
        OperadorNome: string,
    }

    DiaOperacional: {
        id: string,
        DiaOperacionalNome: string,
    }

    CategoriaLinha: {
        id: string,
        CategoriaLinhaNome: string,
    }

    Acessibilidade: boolean

    Tarifa: number

    NomeLinha: string

    CodigoLinha: string

    ViagemA: {
        Origem: string

        Destino: string

        Horarios: any[]

        Vias: [
            {
                ViaNome: string
                ViaHorarios: any[]
            }
        ]
    }


    ViagemB: {
        Origem: string

        Destino: string

        Horarios: any

        Vias: [
            {
                ViaNome: string
                ViaHorarios: any
            }
        ]
    }
}

make the instance of my model so

private model: Linhamodel = new Linhamodel();

and when I try to access my property with this.model.Viagema.Origin is not accepted! someone knows how to do this?

1 answer

1


The problem is that the Viagema property is not ready for use (Viagema = Undefined), as no instance has been defined in it. Before using it, you need to assign an object that follows the established contract/format, that is, an object that has members: Origin (string), Destination (string), Horarios (array), Vias (array of type X).

The solution, to avoid receiving Undefined errors, would be to create a constructor in the Linhamodel class to initialize the Viagema property. For example:

export class LinhaModel {

    constructor()
    {
        this.ViagemA = {};
    }

   ViagemA: {
    Origem: string

    Destino: string

    Horarios: any[]

    Vias: [
        {
            ViaNome: string
            ViaHorarios: any[]
        }
    ]
}


}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.