0

Can anyone help me please? I have a typescript model and i want to use a method which is in that model but when I do {{myvar.ContractNumber.GetCompanyContractNumber()}} I have nothing. My method is not called. Here is the model:

export class ContractNumber {
    constructor() {
        this.Prefixes = new Array<string>();
        this.Suffixes = new Array<string>();
    }

    public Prefixes: string[];
    public CompanyNumber: string;
    public Number: string;
    public Suffixes: string[];

    public Init(): void {
        this.Prefixes = new Array<string>();
        this.Suffixes = new Array<string>();
    }

    public DisplayPrefix(separator: string = ""): string {
        var str = "";
        var max = this.Prefixes.length;
        for (var index = 0; index < max; index++) {
            if (index + 1 == max)
                str += this.Prefixes[index];
            else
                str += this.Prefixes[index] + separator;
        }
        return str;
    }

    public DisplaySuffix(separator: string = ""): string 
    {
        var str = "";
        var max = this.Suffixes.length;
        for (var index = 0; index < max; index++) {
            if (index + 1 == max)
                str += this.Suffixes[index];
            else
                str += this.Suffixes[index] + separator;
        }
        return str;
    }

    public GetContractNumber(): string 
    {
        return this.DisplayPrefix() + Number + this.DisplaySuffix();
    }

    public GetCompanyContractNumber(): string {
        var str = "";
        for (var index = 0; index < this.Prefixes.length; index++) {
            str += this.Prefixes[index];
        }
        str += this.CompanyNumber != null
            ? this.CompanyNumber
            : this.Number;

        for (var index = 0; index < this.Suffixes.length; index++) {
            str += this.Suffixes[index];
        }
        return str;
    }
    public cn = () => {
        var str = "";
        for (var index = 0; index < this.Prefixes.length; index++) {
            str += this.Prefixes[index];
        }
        str += this.CompanyNumber != null
            ? this.CompanyNumber
            : this.Number;

        for (var index = 0; index < this.Suffixes.length; index++) {
            str += this.Suffixes[index];
        }
        return str;
    } 
}

Thank you.

Kostya Shkryob
  • 2,344
  • 17
  • 24
florea_g
  • 1
  • 3
  • Did you try myvar?.ContractNumber?.GetCompanyContractNumber() in your template. – bakerhumadi Apr 10 '17 at 15:55
  • 2
    Can you add the code portion of your component that contains myvar variable initialization? `ContractNumber` is a class, not exactly sure how you managed to do `myvar.ContractNumber...`. Would of made sense if it was `myvar.GetCompanyContractNumber()` when you new up the `ContractNumber` object. – penleychan Apr 10 '17 at 16:04
  • @bakerhumadi is not working.. – florea_g Apr 10 '17 at 16:09
  • @12seconds I have a class: `export class Contract { ... public ContractNumber:ContractNumber; ... }` and i have a api which returns me a list of Contracts so in my template i am doing this: `
    `
    – florea_g Apr 10 '17 at 16:13
  • @Sparky, did you check if either `cashingContracts` has objects in it? and that your property `ContractNumber` is not null. – penleychan Apr 10 '17 at 16:24
  • @12seconds Yes they are not null. if i write the same method in my component and i call like `{{getContractNumber(cashingContract)}}` it works but is not the best thing to do. – florea_g Apr 10 '17 at 16:32
  • @florea_g as a sidecomment, it's really quite bad practice to call functions in template. It could cause an infinite loop, since that function would be called every time change detection is run by Angular. So you'd want to rethink that ;) but here's one issue: http://stackoverflow.com/questions/43081810/angular2-infinite-loop-when-i-call-method-from-a-angular-2-class-inside-templa – AT82 Apr 11 '17 at 11:52

0 Answers0