Angular/TypeScript: _v.context.$implicit.getcolor is not a function












0















I'm working on an Angular 7 project and have run in to a weird situation that I didnt expect. A bit new to Angular and TypeScript..
I want to use one of 4 different css classes in my html. The class name is decided based on a calculation in the location.ts file. Not something complicated but still frustrating why I cant use angular/typescript the way I thought I could. What is the proper way to do something like this?



location.ts
Its the getcolor() function that I try to reach from my html.



export class Location {
location: string;
name: string;
current: number;
max: number;
isActive: boolean = false;
color: string;

getcolor() {
let fraction = this.current / this.max;
switch (true) {
case (fraction < 0.25): { return "empty"; }
case (fraction < 0.5): { return "half-empty"; }
case (fraction < 0.75): { return "half-full"; }
case (fraction < 1): { return "full"; }
default: { return ""; }
}
}
}


location.service.ts



  getLocationList(): Observable<Location> {
let list = this.http.get<Location>(this.url + "locations/");
return list;
};


map.component.ts



constructor(private locationService: LocationService) { }

public locationList: Location;

ngOnInit() {
this.locationService.getLocationList().subscribe(e => this.locationList = e);
}


map.component.html
Each card should have one of the four different css styles. Might not be the way to do it but for now we just need something to work. Any suggestions to a more correct approach is appreciated :) I jsut try to call the method on the Location class to get the name of the css style to use.



<div *ngFor="let loc of locationList" class="card {{loc.getcolor()}}">
<span><strong>{{loc.location}}</strong> - {{loc.name}}</span>
<div>
Storage: {{loc.current}}/{{loc.max}} tons
</div>
<div>
Remaining: {{loc.max - loc.current}} tons
</div>
</div>









share|improve this question

























  • stackoverflow.com/questions/22875636/…

    – yurzui
    Nov 16 '18 at 9:20











  • Please show what the /locations endpoint's result looks like. Also what is the initial value of localionList?

    – Jeto
    Nov 16 '18 at 9:29











  • Added some more code from the map.component.ts. Hope this helps.

    – Christian
    Nov 16 '18 at 9:42
















0















I'm working on an Angular 7 project and have run in to a weird situation that I didnt expect. A bit new to Angular and TypeScript..
I want to use one of 4 different css classes in my html. The class name is decided based on a calculation in the location.ts file. Not something complicated but still frustrating why I cant use angular/typescript the way I thought I could. What is the proper way to do something like this?



location.ts
Its the getcolor() function that I try to reach from my html.



export class Location {
location: string;
name: string;
current: number;
max: number;
isActive: boolean = false;
color: string;

getcolor() {
let fraction = this.current / this.max;
switch (true) {
case (fraction < 0.25): { return "empty"; }
case (fraction < 0.5): { return "half-empty"; }
case (fraction < 0.75): { return "half-full"; }
case (fraction < 1): { return "full"; }
default: { return ""; }
}
}
}


location.service.ts



  getLocationList(): Observable<Location> {
let list = this.http.get<Location>(this.url + "locations/");
return list;
};


map.component.ts



constructor(private locationService: LocationService) { }

public locationList: Location;

ngOnInit() {
this.locationService.getLocationList().subscribe(e => this.locationList = e);
}


map.component.html
Each card should have one of the four different css styles. Might not be the way to do it but for now we just need something to work. Any suggestions to a more correct approach is appreciated :) I jsut try to call the method on the Location class to get the name of the css style to use.



<div *ngFor="let loc of locationList" class="card {{loc.getcolor()}}">
<span><strong>{{loc.location}}</strong> - {{loc.name}}</span>
<div>
Storage: {{loc.current}}/{{loc.max}} tons
</div>
<div>
Remaining: {{loc.max - loc.current}} tons
</div>
</div>









share|improve this question

























  • stackoverflow.com/questions/22875636/…

    – yurzui
    Nov 16 '18 at 9:20











  • Please show what the /locations endpoint's result looks like. Also what is the initial value of localionList?

    – Jeto
    Nov 16 '18 at 9:29











  • Added some more code from the map.component.ts. Hope this helps.

    – Christian
    Nov 16 '18 at 9:42














0












0








0








I'm working on an Angular 7 project and have run in to a weird situation that I didnt expect. A bit new to Angular and TypeScript..
I want to use one of 4 different css classes in my html. The class name is decided based on a calculation in the location.ts file. Not something complicated but still frustrating why I cant use angular/typescript the way I thought I could. What is the proper way to do something like this?



location.ts
Its the getcolor() function that I try to reach from my html.



export class Location {
location: string;
name: string;
current: number;
max: number;
isActive: boolean = false;
color: string;

getcolor() {
let fraction = this.current / this.max;
switch (true) {
case (fraction < 0.25): { return "empty"; }
case (fraction < 0.5): { return "half-empty"; }
case (fraction < 0.75): { return "half-full"; }
case (fraction < 1): { return "full"; }
default: { return ""; }
}
}
}


location.service.ts



  getLocationList(): Observable<Location> {
let list = this.http.get<Location>(this.url + "locations/");
return list;
};


map.component.ts



constructor(private locationService: LocationService) { }

public locationList: Location;

ngOnInit() {
this.locationService.getLocationList().subscribe(e => this.locationList = e);
}


map.component.html
Each card should have one of the four different css styles. Might not be the way to do it but for now we just need something to work. Any suggestions to a more correct approach is appreciated :) I jsut try to call the method on the Location class to get the name of the css style to use.



<div *ngFor="let loc of locationList" class="card {{loc.getcolor()}}">
<span><strong>{{loc.location}}</strong> - {{loc.name}}</span>
<div>
Storage: {{loc.current}}/{{loc.max}} tons
</div>
<div>
Remaining: {{loc.max - loc.current}} tons
</div>
</div>









share|improve this question
















I'm working on an Angular 7 project and have run in to a weird situation that I didnt expect. A bit new to Angular and TypeScript..
I want to use one of 4 different css classes in my html. The class name is decided based on a calculation in the location.ts file. Not something complicated but still frustrating why I cant use angular/typescript the way I thought I could. What is the proper way to do something like this?



location.ts
Its the getcolor() function that I try to reach from my html.



export class Location {
location: string;
name: string;
current: number;
max: number;
isActive: boolean = false;
color: string;

getcolor() {
let fraction = this.current / this.max;
switch (true) {
case (fraction < 0.25): { return "empty"; }
case (fraction < 0.5): { return "half-empty"; }
case (fraction < 0.75): { return "half-full"; }
case (fraction < 1): { return "full"; }
default: { return ""; }
}
}
}


location.service.ts



  getLocationList(): Observable<Location> {
let list = this.http.get<Location>(this.url + "locations/");
return list;
};


map.component.ts



constructor(private locationService: LocationService) { }

public locationList: Location;

ngOnInit() {
this.locationService.getLocationList().subscribe(e => this.locationList = e);
}


map.component.html
Each card should have one of the four different css styles. Might not be the way to do it but for now we just need something to work. Any suggestions to a more correct approach is appreciated :) I jsut try to call the method on the Location class to get the name of the css style to use.



<div *ngFor="let loc of locationList" class="card {{loc.getcolor()}}">
<span><strong>{{loc.location}}</strong> - {{loc.name}}</span>
<div>
Storage: {{loc.current}}/{{loc.max}} tons
</div>
<div>
Remaining: {{loc.max - loc.current}} tons
</div>
</div>






angular typescript angular7






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 6 '18 at 8:47









Goncalo Peres

1,4771720




1,4771720










asked Nov 16 '18 at 9:18









ChristianChristian

32211032




32211032













  • stackoverflow.com/questions/22875636/…

    – yurzui
    Nov 16 '18 at 9:20











  • Please show what the /locations endpoint's result looks like. Also what is the initial value of localionList?

    – Jeto
    Nov 16 '18 at 9:29











  • Added some more code from the map.component.ts. Hope this helps.

    – Christian
    Nov 16 '18 at 9:42



















  • stackoverflow.com/questions/22875636/…

    – yurzui
    Nov 16 '18 at 9:20











  • Please show what the /locations endpoint's result looks like. Also what is the initial value of localionList?

    – Jeto
    Nov 16 '18 at 9:29











  • Added some more code from the map.component.ts. Hope this helps.

    – Christian
    Nov 16 '18 at 9:42

















stackoverflow.com/questions/22875636/…

– yurzui
Nov 16 '18 at 9:20





stackoverflow.com/questions/22875636/…

– yurzui
Nov 16 '18 at 9:20













Please show what the /locations endpoint's result looks like. Also what is the initial value of localionList?

– Jeto
Nov 16 '18 at 9:29





Please show what the /locations endpoint's result looks like. Also what is the initial value of localionList?

– Jeto
Nov 16 '18 at 9:29













Added some more code from the map.component.ts. Hope this helps.

– Christian
Nov 16 '18 at 9:42





Added some more code from the map.component.ts. Hope this helps.

– Christian
Nov 16 '18 at 9:42












2 Answers
2






active

oldest

votes


















0














You are trying to call your function getColor before getting your subscribe answer. You should do two things :




  • Ensure locationList is an array of type Location (you didn't show your attribut initialisation e.g. private locationList: Array<Location>

  • Check that loc is not undefined


Last but not least you should use NgClass to bind a class !






share|improve this answer
























  • If I remove the getcolor thing, then it works fine. List is populated and they are showing up.

    – Christian
    Nov 16 '18 at 9:44











  • Ok then and if you try to use [ngClass]="loc.getcolor" ? And you are sure there is no error with the camel case ? (it is all in lower)

    – H. Gybels
    Nov 16 '18 at 9:54











  • No dosent work. Cant use the getcolor() anywhere.

    – Christian
    Nov 16 '18 at 10:16











  • I think in you subscribe when you are casting this.locationList = e he is taking the JSONObject and not a Location anymore, try to add a new Location with e.parameters in constructor

    – H. Gybels
    Nov 16 '18 at 10:18



















0














Okay so I found a solution! :)



location.service.tsexport class LocationService {



  constructor(private http: HttpClient) { }
public url = "/api/location/"
getLocationList(): Observable<Location> {
return this.http.get<Location>(this.url + "locations/").pipe(map(x => x.map(y => Location.fromJSON(y))));
};
}


location.ts
Added this static function to the location.ts



static fromJSON(data: any) {
return Object.assign(new this, data);
}


map.component.ts
This is the same as before.



  constructor(private locationService: LocationService) { }

locationList: Location;
selectedLocation: Location;

ngOnInit() {
this.locationService.getLocationList().subscribe(e => this.locationList = e);
}


map.component.html Now I can get the color out :)



<div *ngFor="let loc of locationList" class="card {{loc.getcolor()}}">
<span><strong>{{loc.location}}</strong> - {{loc.name}}</span>
<div>
Storage: {{loc.current}}/{{loc.max}} tons
</div>
<div>
Remaining: {{loc.max - loc.current}} tons
</div>
</div>





share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53334769%2fangular-typescript-v-context-implicit-getcolor-is-not-a-function%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You are trying to call your function getColor before getting your subscribe answer. You should do two things :




    • Ensure locationList is an array of type Location (you didn't show your attribut initialisation e.g. private locationList: Array<Location>

    • Check that loc is not undefined


    Last but not least you should use NgClass to bind a class !






    share|improve this answer
























    • If I remove the getcolor thing, then it works fine. List is populated and they are showing up.

      – Christian
      Nov 16 '18 at 9:44











    • Ok then and if you try to use [ngClass]="loc.getcolor" ? And you are sure there is no error with the camel case ? (it is all in lower)

      – H. Gybels
      Nov 16 '18 at 9:54











    • No dosent work. Cant use the getcolor() anywhere.

      – Christian
      Nov 16 '18 at 10:16











    • I think in you subscribe when you are casting this.locationList = e he is taking the JSONObject and not a Location anymore, try to add a new Location with e.parameters in constructor

      – H. Gybels
      Nov 16 '18 at 10:18
















    0














    You are trying to call your function getColor before getting your subscribe answer. You should do two things :




    • Ensure locationList is an array of type Location (you didn't show your attribut initialisation e.g. private locationList: Array<Location>

    • Check that loc is not undefined


    Last but not least you should use NgClass to bind a class !






    share|improve this answer
























    • If I remove the getcolor thing, then it works fine. List is populated and they are showing up.

      – Christian
      Nov 16 '18 at 9:44











    • Ok then and if you try to use [ngClass]="loc.getcolor" ? And you are sure there is no error with the camel case ? (it is all in lower)

      – H. Gybels
      Nov 16 '18 at 9:54











    • No dosent work. Cant use the getcolor() anywhere.

      – Christian
      Nov 16 '18 at 10:16











    • I think in you subscribe when you are casting this.locationList = e he is taking the JSONObject and not a Location anymore, try to add a new Location with e.parameters in constructor

      – H. Gybels
      Nov 16 '18 at 10:18














    0












    0








    0







    You are trying to call your function getColor before getting your subscribe answer. You should do two things :




    • Ensure locationList is an array of type Location (you didn't show your attribut initialisation e.g. private locationList: Array<Location>

    • Check that loc is not undefined


    Last but not least you should use NgClass to bind a class !






    share|improve this answer













    You are trying to call your function getColor before getting your subscribe answer. You should do two things :




    • Ensure locationList is an array of type Location (you didn't show your attribut initialisation e.g. private locationList: Array<Location>

    • Check that loc is not undefined


    Last but not least you should use NgClass to bind a class !







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 16 '18 at 9:28









    H. GybelsH. Gybels

    7711




    7711













    • If I remove the getcolor thing, then it works fine. List is populated and they are showing up.

      – Christian
      Nov 16 '18 at 9:44











    • Ok then and if you try to use [ngClass]="loc.getcolor" ? And you are sure there is no error with the camel case ? (it is all in lower)

      – H. Gybels
      Nov 16 '18 at 9:54











    • No dosent work. Cant use the getcolor() anywhere.

      – Christian
      Nov 16 '18 at 10:16











    • I think in you subscribe when you are casting this.locationList = e he is taking the JSONObject and not a Location anymore, try to add a new Location with e.parameters in constructor

      – H. Gybels
      Nov 16 '18 at 10:18



















    • If I remove the getcolor thing, then it works fine. List is populated and they are showing up.

      – Christian
      Nov 16 '18 at 9:44











    • Ok then and if you try to use [ngClass]="loc.getcolor" ? And you are sure there is no error with the camel case ? (it is all in lower)

      – H. Gybels
      Nov 16 '18 at 9:54











    • No dosent work. Cant use the getcolor() anywhere.

      – Christian
      Nov 16 '18 at 10:16











    • I think in you subscribe when you are casting this.locationList = e he is taking the JSONObject and not a Location anymore, try to add a new Location with e.parameters in constructor

      – H. Gybels
      Nov 16 '18 at 10:18

















    If I remove the getcolor thing, then it works fine. List is populated and they are showing up.

    – Christian
    Nov 16 '18 at 9:44





    If I remove the getcolor thing, then it works fine. List is populated and they are showing up.

    – Christian
    Nov 16 '18 at 9:44













    Ok then and if you try to use [ngClass]="loc.getcolor" ? And you are sure there is no error with the camel case ? (it is all in lower)

    – H. Gybels
    Nov 16 '18 at 9:54





    Ok then and if you try to use [ngClass]="loc.getcolor" ? And you are sure there is no error with the camel case ? (it is all in lower)

    – H. Gybels
    Nov 16 '18 at 9:54













    No dosent work. Cant use the getcolor() anywhere.

    – Christian
    Nov 16 '18 at 10:16





    No dosent work. Cant use the getcolor() anywhere.

    – Christian
    Nov 16 '18 at 10:16













    I think in you subscribe when you are casting this.locationList = e he is taking the JSONObject and not a Location anymore, try to add a new Location with e.parameters in constructor

    – H. Gybels
    Nov 16 '18 at 10:18





    I think in you subscribe when you are casting this.locationList = e he is taking the JSONObject and not a Location anymore, try to add a new Location with e.parameters in constructor

    – H. Gybels
    Nov 16 '18 at 10:18













    0














    Okay so I found a solution! :)



    location.service.tsexport class LocationService {



      constructor(private http: HttpClient) { }
    public url = "/api/location/"
    getLocationList(): Observable<Location> {
    return this.http.get<Location>(this.url + "locations/").pipe(map(x => x.map(y => Location.fromJSON(y))));
    };
    }


    location.ts
    Added this static function to the location.ts



    static fromJSON(data: any) {
    return Object.assign(new this, data);
    }


    map.component.ts
    This is the same as before.



      constructor(private locationService: LocationService) { }

    locationList: Location;
    selectedLocation: Location;

    ngOnInit() {
    this.locationService.getLocationList().subscribe(e => this.locationList = e);
    }


    map.component.html Now I can get the color out :)



    <div *ngFor="let loc of locationList" class="card {{loc.getcolor()}}">
    <span><strong>{{loc.location}}</strong> - {{loc.name}}</span>
    <div>
    Storage: {{loc.current}}/{{loc.max}} tons
    </div>
    <div>
    Remaining: {{loc.max - loc.current}} tons
    </div>
    </div>





    share|improve this answer




























      0














      Okay so I found a solution! :)



      location.service.tsexport class LocationService {



        constructor(private http: HttpClient) { }
      public url = "/api/location/"
      getLocationList(): Observable<Location> {
      return this.http.get<Location>(this.url + "locations/").pipe(map(x => x.map(y => Location.fromJSON(y))));
      };
      }


      location.ts
      Added this static function to the location.ts



      static fromJSON(data: any) {
      return Object.assign(new this, data);
      }


      map.component.ts
      This is the same as before.



        constructor(private locationService: LocationService) { }

      locationList: Location;
      selectedLocation: Location;

      ngOnInit() {
      this.locationService.getLocationList().subscribe(e => this.locationList = e);
      }


      map.component.html Now I can get the color out :)



      <div *ngFor="let loc of locationList" class="card {{loc.getcolor()}}">
      <span><strong>{{loc.location}}</strong> - {{loc.name}}</span>
      <div>
      Storage: {{loc.current}}/{{loc.max}} tons
      </div>
      <div>
      Remaining: {{loc.max - loc.current}} tons
      </div>
      </div>





      share|improve this answer


























        0












        0








        0







        Okay so I found a solution! :)



        location.service.tsexport class LocationService {



          constructor(private http: HttpClient) { }
        public url = "/api/location/"
        getLocationList(): Observable<Location> {
        return this.http.get<Location>(this.url + "locations/").pipe(map(x => x.map(y => Location.fromJSON(y))));
        };
        }


        location.ts
        Added this static function to the location.ts



        static fromJSON(data: any) {
        return Object.assign(new this, data);
        }


        map.component.ts
        This is the same as before.



          constructor(private locationService: LocationService) { }

        locationList: Location;
        selectedLocation: Location;

        ngOnInit() {
        this.locationService.getLocationList().subscribe(e => this.locationList = e);
        }


        map.component.html Now I can get the color out :)



        <div *ngFor="let loc of locationList" class="card {{loc.getcolor()}}">
        <span><strong>{{loc.location}}</strong> - {{loc.name}}</span>
        <div>
        Storage: {{loc.current}}/{{loc.max}} tons
        </div>
        <div>
        Remaining: {{loc.max - loc.current}} tons
        </div>
        </div>





        share|improve this answer













        Okay so I found a solution! :)



        location.service.tsexport class LocationService {



          constructor(private http: HttpClient) { }
        public url = "/api/location/"
        getLocationList(): Observable<Location> {
        return this.http.get<Location>(this.url + "locations/").pipe(map(x => x.map(y => Location.fromJSON(y))));
        };
        }


        location.ts
        Added this static function to the location.ts



        static fromJSON(data: any) {
        return Object.assign(new this, data);
        }


        map.component.ts
        This is the same as before.



          constructor(private locationService: LocationService) { }

        locationList: Location;
        selectedLocation: Location;

        ngOnInit() {
        this.locationService.getLocationList().subscribe(e => this.locationList = e);
        }


        map.component.html Now I can get the color out :)



        <div *ngFor="let loc of locationList" class="card {{loc.getcolor()}}">
        <span><strong>{{loc.location}}</strong> - {{loc.name}}</span>
        <div>
        Storage: {{loc.current}}/{{loc.max}} tons
        </div>
        <div>
        Remaining: {{loc.max - loc.current}} tons
        </div>
        </div>






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 16 '18 at 12:41









        ChristianChristian

        32211032




        32211032






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53334769%2fangular-typescript-v-context-implicit-getcolor-is-not-a-function%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Florida Star v. B. J. F.

            Danny Elfman

            Lugert, Oklahoma