Angular 6 *ngFor display different styles for first, odd, even and last










1














I am learning Angular 6 and just trying to put togheter some of the stuff I have learned and I am currently running into an issue that I cannot find an answer to. I am trying to change the style of a LI using *ngFor depending if the index is First, Last, Odd or Even. So far everything works but I can't figure out how to do it for the Last because everything I add a new object to my list, it is obviously the last so it render the color for the last.



I understand how to do it but the real problem is that I am adding stuff dynamicly to my list from a form and I'm not sure how to evaluate the Last so that the others become to right color.



Keep in mind that I am still a newb and it might look messy and I also understand that some client-side validations I am doing are probably not optimal or required since HTMl5 but I made it to learn.



Here is my code for my component HTML



>



<h1>List of courses :</h1><br>
<div *ngIf="courses.length > 0; then coursesList else noCourses"></div>

<ng-template #coursesList>
<h2>List of Courses :</h2>
<ul *ngFor="let course of courses; index as i;">
<li [ngStyle]="'background-color':getColor(i)" style="color: white;">
<strong>Index : </strong>i <strong>ID : </strong>course.id <strong>Name</strong> : course.name
<button (click)="onRemove(i)">Remove</button>
<button (click)="onModify(i)">Modify</button>
</li>
</ul>
</ng-template>
<ng-template #noCourses>
<h5>There are no courses in this list. Use the form bellow to add some.</h5>
</ng-template>

<div (keyup.enter)="onAdd()">
<span>ID : <input type="number" (keypress)="checkNumber($event)" [(ngModel)]="fields.id" placeholder="Enter an ID"></span>
<span>Name : <input type="text" [(ngModel)]="fields.name" placeholder="Enter a NAME"></span>
<button (click)="onAdd()">Add</button>
<button (click)="onClear()">Clear</button>
</div>
<div *ngIf="isNotNumber" style="background-color: red; color:black"><strong>ID can only be numbers !</strong></div>
<div *ngIf="noValues" style="background-color: red; color:black"><strong>Please fill all fields !</strong></div>
<div *ngIf="noModifyValues" style="background-color: red; color:black"><strong>To modify enter all informations!</strong></div>


Code for .TS



>



import Component from '@angular/core';

@Component(
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
)


export class AppComponent
noValues: boolean;
noModifyValues: boolean;
isNotNumber: boolean;
fields: Courses = id: null, name: null;
courses: Array<Courses> = ;
viewMode: string = null;

checkNumber($event)
if ($event.keyCode != 13)
isFinite($event.key) ? this.isNotNumber = false : this.isNotNumber = true;



onAdd() !this.fields.name)
this.noValues = true;
else
this.courses.push(id: this.fields.id, name: this.fields.name);
this.fields.id = null;
this.fields.name = null;
this.noValues = false;



onRemove(i)
this.courses.splice(i, 1);


onClear()
this.courses = ;
this.fields.id = null;
this.fields.name = null;
this.noValues = false;


onModify(i) !this.fields.name)
this.noModifyValues = true;
else
this.courses[i].name = this.fields.name;
this.courses[i].id = this.fields.id;
this.noModifyValues = false;




getColor(i)
if (i % 2 === 0 && i != 0)i = 'odd';
switch (i)
case i = 0 : return 'orange';
case i = 'odd' : return 'blue';

return 'red';


interface Courses
id: number;
name: string;



Image of the code in action for better understanding.










share|improve this question





















  • If you only want cange the background-color you can use [style.background-color] and you can use ternary operator in the .html, see my answer
    – Eliseo
    Nov 13 '18 at 8:15















1














I am learning Angular 6 and just trying to put togheter some of the stuff I have learned and I am currently running into an issue that I cannot find an answer to. I am trying to change the style of a LI using *ngFor depending if the index is First, Last, Odd or Even. So far everything works but I can't figure out how to do it for the Last because everything I add a new object to my list, it is obviously the last so it render the color for the last.



I understand how to do it but the real problem is that I am adding stuff dynamicly to my list from a form and I'm not sure how to evaluate the Last so that the others become to right color.



Keep in mind that I am still a newb and it might look messy and I also understand that some client-side validations I am doing are probably not optimal or required since HTMl5 but I made it to learn.



Here is my code for my component HTML



>



<h1>List of courses :</h1><br>
<div *ngIf="courses.length > 0; then coursesList else noCourses"></div>

<ng-template #coursesList>
<h2>List of Courses :</h2>
<ul *ngFor="let course of courses; index as i;">
<li [ngStyle]="'background-color':getColor(i)" style="color: white;">
<strong>Index : </strong>i <strong>ID : </strong>course.id <strong>Name</strong> : course.name
<button (click)="onRemove(i)">Remove</button>
<button (click)="onModify(i)">Modify</button>
</li>
</ul>
</ng-template>
<ng-template #noCourses>
<h5>There are no courses in this list. Use the form bellow to add some.</h5>
</ng-template>

<div (keyup.enter)="onAdd()">
<span>ID : <input type="number" (keypress)="checkNumber($event)" [(ngModel)]="fields.id" placeholder="Enter an ID"></span>
<span>Name : <input type="text" [(ngModel)]="fields.name" placeholder="Enter a NAME"></span>
<button (click)="onAdd()">Add</button>
<button (click)="onClear()">Clear</button>
</div>
<div *ngIf="isNotNumber" style="background-color: red; color:black"><strong>ID can only be numbers !</strong></div>
<div *ngIf="noValues" style="background-color: red; color:black"><strong>Please fill all fields !</strong></div>
<div *ngIf="noModifyValues" style="background-color: red; color:black"><strong>To modify enter all informations!</strong></div>


Code for .TS



>



import Component from '@angular/core';

@Component(
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
)


export class AppComponent
noValues: boolean;
noModifyValues: boolean;
isNotNumber: boolean;
fields: Courses = id: null, name: null;
courses: Array<Courses> = ;
viewMode: string = null;

checkNumber($event)
if ($event.keyCode != 13)
isFinite($event.key) ? this.isNotNumber = false : this.isNotNumber = true;



onAdd() !this.fields.name)
this.noValues = true;
else
this.courses.push(id: this.fields.id, name: this.fields.name);
this.fields.id = null;
this.fields.name = null;
this.noValues = false;



onRemove(i)
this.courses.splice(i, 1);


onClear()
this.courses = ;
this.fields.id = null;
this.fields.name = null;
this.noValues = false;


onModify(i) !this.fields.name)
this.noModifyValues = true;
else
this.courses[i].name = this.fields.name;
this.courses[i].id = this.fields.id;
this.noModifyValues = false;




getColor(i)
if (i % 2 === 0 && i != 0)i = 'odd';
switch (i)
case i = 0 : return 'orange';
case i = 'odd' : return 'blue';

return 'red';


interface Courses
id: number;
name: string;



Image of the code in action for better understanding.










share|improve this question





















  • If you only want cange the background-color you can use [style.background-color] and you can use ternary operator in the .html, see my answer
    – Eliseo
    Nov 13 '18 at 8:15













1












1








1







I am learning Angular 6 and just trying to put togheter some of the stuff I have learned and I am currently running into an issue that I cannot find an answer to. I am trying to change the style of a LI using *ngFor depending if the index is First, Last, Odd or Even. So far everything works but I can't figure out how to do it for the Last because everything I add a new object to my list, it is obviously the last so it render the color for the last.



I understand how to do it but the real problem is that I am adding stuff dynamicly to my list from a form and I'm not sure how to evaluate the Last so that the others become to right color.



Keep in mind that I am still a newb and it might look messy and I also understand that some client-side validations I am doing are probably not optimal or required since HTMl5 but I made it to learn.



Here is my code for my component HTML



>



<h1>List of courses :</h1><br>
<div *ngIf="courses.length > 0; then coursesList else noCourses"></div>

<ng-template #coursesList>
<h2>List of Courses :</h2>
<ul *ngFor="let course of courses; index as i;">
<li [ngStyle]="'background-color':getColor(i)" style="color: white;">
<strong>Index : </strong>i <strong>ID : </strong>course.id <strong>Name</strong> : course.name
<button (click)="onRemove(i)">Remove</button>
<button (click)="onModify(i)">Modify</button>
</li>
</ul>
</ng-template>
<ng-template #noCourses>
<h5>There are no courses in this list. Use the form bellow to add some.</h5>
</ng-template>

<div (keyup.enter)="onAdd()">
<span>ID : <input type="number" (keypress)="checkNumber($event)" [(ngModel)]="fields.id" placeholder="Enter an ID"></span>
<span>Name : <input type="text" [(ngModel)]="fields.name" placeholder="Enter a NAME"></span>
<button (click)="onAdd()">Add</button>
<button (click)="onClear()">Clear</button>
</div>
<div *ngIf="isNotNumber" style="background-color: red; color:black"><strong>ID can only be numbers !</strong></div>
<div *ngIf="noValues" style="background-color: red; color:black"><strong>Please fill all fields !</strong></div>
<div *ngIf="noModifyValues" style="background-color: red; color:black"><strong>To modify enter all informations!</strong></div>


Code for .TS



>



import Component from '@angular/core';

@Component(
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
)


export class AppComponent
noValues: boolean;
noModifyValues: boolean;
isNotNumber: boolean;
fields: Courses = id: null, name: null;
courses: Array<Courses> = ;
viewMode: string = null;

checkNumber($event)
if ($event.keyCode != 13)
isFinite($event.key) ? this.isNotNumber = false : this.isNotNumber = true;



onAdd() !this.fields.name)
this.noValues = true;
else
this.courses.push(id: this.fields.id, name: this.fields.name);
this.fields.id = null;
this.fields.name = null;
this.noValues = false;



onRemove(i)
this.courses.splice(i, 1);


onClear()
this.courses = ;
this.fields.id = null;
this.fields.name = null;
this.noValues = false;


onModify(i) !this.fields.name)
this.noModifyValues = true;
else
this.courses[i].name = this.fields.name;
this.courses[i].id = this.fields.id;
this.noModifyValues = false;




getColor(i)
if (i % 2 === 0 && i != 0)i = 'odd';
switch (i)
case i = 0 : return 'orange';
case i = 'odd' : return 'blue';

return 'red';


interface Courses
id: number;
name: string;



Image of the code in action for better understanding.










share|improve this question













I am learning Angular 6 and just trying to put togheter some of the stuff I have learned and I am currently running into an issue that I cannot find an answer to. I am trying to change the style of a LI using *ngFor depending if the index is First, Last, Odd or Even. So far everything works but I can't figure out how to do it for the Last because everything I add a new object to my list, it is obviously the last so it render the color for the last.



I understand how to do it but the real problem is that I am adding stuff dynamicly to my list from a form and I'm not sure how to evaluate the Last so that the others become to right color.



Keep in mind that I am still a newb and it might look messy and I also understand that some client-side validations I am doing are probably not optimal or required since HTMl5 but I made it to learn.



Here is my code for my component HTML



>



<h1>List of courses :</h1><br>
<div *ngIf="courses.length > 0; then coursesList else noCourses"></div>

<ng-template #coursesList>
<h2>List of Courses :</h2>
<ul *ngFor="let course of courses; index as i;">
<li [ngStyle]="'background-color':getColor(i)" style="color: white;">
<strong>Index : </strong>i <strong>ID : </strong>course.id <strong>Name</strong> : course.name
<button (click)="onRemove(i)">Remove</button>
<button (click)="onModify(i)">Modify</button>
</li>
</ul>
</ng-template>
<ng-template #noCourses>
<h5>There are no courses in this list. Use the form bellow to add some.</h5>
</ng-template>

<div (keyup.enter)="onAdd()">
<span>ID : <input type="number" (keypress)="checkNumber($event)" [(ngModel)]="fields.id" placeholder="Enter an ID"></span>
<span>Name : <input type="text" [(ngModel)]="fields.name" placeholder="Enter a NAME"></span>
<button (click)="onAdd()">Add</button>
<button (click)="onClear()">Clear</button>
</div>
<div *ngIf="isNotNumber" style="background-color: red; color:black"><strong>ID can only be numbers !</strong></div>
<div *ngIf="noValues" style="background-color: red; color:black"><strong>Please fill all fields !</strong></div>
<div *ngIf="noModifyValues" style="background-color: red; color:black"><strong>To modify enter all informations!</strong></div>


Code for .TS



>



import Component from '@angular/core';

@Component(
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
)


export class AppComponent
noValues: boolean;
noModifyValues: boolean;
isNotNumber: boolean;
fields: Courses = id: null, name: null;
courses: Array<Courses> = ;
viewMode: string = null;

checkNumber($event)
if ($event.keyCode != 13)
isFinite($event.key) ? this.isNotNumber = false : this.isNotNumber = true;



onAdd() !this.fields.name)
this.noValues = true;
else
this.courses.push(id: this.fields.id, name: this.fields.name);
this.fields.id = null;
this.fields.name = null;
this.noValues = false;



onRemove(i)
this.courses.splice(i, 1);


onClear()
this.courses = ;
this.fields.id = null;
this.fields.name = null;
this.noValues = false;


onModify(i) !this.fields.name)
this.noModifyValues = true;
else
this.courses[i].name = this.fields.name;
this.courses[i].id = this.fields.id;
this.noModifyValues = false;




getColor(i)
if (i % 2 === 0 && i != 0)i = 'odd';
switch (i)
case i = 0 : return 'orange';
case i = 'odd' : return 'blue';

return 'red';


interface Courses
id: number;
name: string;



Image of the code in action for better understanding.







arrays angular typescript angular6






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 3:01









Jean-Philippe DufourJean-Philippe Dufour

297




297











  • If you only want cange the background-color you can use [style.background-color] and you can use ternary operator in the .html, see my answer
    – Eliseo
    Nov 13 '18 at 8:15
















  • If you only want cange the background-color you can use [style.background-color] and you can use ternary operator in the .html, see my answer
    – Eliseo
    Nov 13 '18 at 8:15















If you only want cange the background-color you can use [style.background-color] and you can use ternary operator in the .html, see my answer
– Eliseo
Nov 13 '18 at 8:15




If you only want cange the background-color you can use [style.background-color] and you can use ternary operator in the .html, see my answer
– Eliseo
Nov 13 '18 at 8:15












4 Answers
4






active

oldest

votes


















1














Try something like this



getColor(i)
if (i % 2 === 0 && i != 0)i = 'odd';
if (this.courses && (this.courses.length - 1 === i)) i = 'last'
switch (i)
case i = 0 : return 'orange';
case i = 'odd' : return 'blue';

return 'red';



Hope it works - Happy coding !!






share|improve this answer




















  • You can use it as else if in case to avoid another condition :)
    – Rahul
    Nov 13 '18 at 3:45


















1














If you only want cange the background-color you can use [style.background-color] and you can use ternary operator in the .html



<ul *ngFor="let course of courses; let index=i;
let odd=odd;
let last=last;
let first=first">
<li [style.backgound-color]="first?'orange':last?'purple':odd?'blue':'red'">
...
</li>
</ul>





share|improve this answer




















  • That's one good way to do it, I wanted to do it this way first but I didn't know you could use more than 1 condition in a short IF on a bind like that. By the way, is there a difference in performance with either ways? I understand this is just a style tho but what if we have ALOT Of values
    – Jean-Philippe Dufour
    Nov 13 '18 at 14:13











  • If you has a lot of values, you can use [className]="first?'class1':... where 'class1', 'class2' are style defining in your component.css. In perfomance a ternary opertor is always faster. I prefer not make a call to a function, but I'm not sure about performance
    – Eliseo
    Nov 16 '18 at 13:14










  • thanks for your input. I will defenitly look this up.
    – Jean-Philippe Dufour
    Nov 17 '18 at 6:06


















0














Thanks Rahul. The part I was missing is evaluating if there is something in courses. However, I had to had a few more lines to Odd and Last as follow :







getColor(i)
if (this.courses && i != 0 && (this.courses.length - 1 === i)) i = 'last'
if (i % 2 === 0 && i != 0 && i != 'last')i = 'odd';
switch (i)
case i = 0 : return 'orange';
case i = 'odd' : return 'blue';
case i = 'last' : return 'purple';

return 'red';



Quick question. It seems like a whole lot of IF and && and checking specific things. Is that the way to do it properly?






share|improve this answer




























    0














    You could use if else ladder instead of mixing up if else and switch and assignments like given below



    getColor(i)

    if(this.courses)

    if(i==0)
    return "orange";
    else if(i==this.courses.length-1)
    return "purple";
    else if (i%2==0)
    return "red";
    else
    return "blue";







    share|improve this answer
















    • 1




      Would def make a cleaner and easier to understand code. Thanks for your input.
      – Jean-Philippe Dufour
      Nov 13 '18 at 5:38










    • When you have simpler alternatives, don't mix up and always welcome :)
      – Mithil Mohan
      Nov 13 '18 at 5:39










    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%2f53273162%2fangular-6-ngfor-display-different-styles-for-first-odd-even-and-last%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Try something like this



    getColor(i)
    if (i % 2 === 0 && i != 0)i = 'odd';
    if (this.courses && (this.courses.length - 1 === i)) i = 'last'
    switch (i)
    case i = 0 : return 'orange';
    case i = 'odd' : return 'blue';

    return 'red';



    Hope it works - Happy coding !!






    share|improve this answer




















    • You can use it as else if in case to avoid another condition :)
      – Rahul
      Nov 13 '18 at 3:45















    1














    Try something like this



    getColor(i)
    if (i % 2 === 0 && i != 0)i = 'odd';
    if (this.courses && (this.courses.length - 1 === i)) i = 'last'
    switch (i)
    case i = 0 : return 'orange';
    case i = 'odd' : return 'blue';

    return 'red';



    Hope it works - Happy coding !!






    share|improve this answer




















    • You can use it as else if in case to avoid another condition :)
      – Rahul
      Nov 13 '18 at 3:45













    1












    1








    1






    Try something like this



    getColor(i)
    if (i % 2 === 0 && i != 0)i = 'odd';
    if (this.courses && (this.courses.length - 1 === i)) i = 'last'
    switch (i)
    case i = 0 : return 'orange';
    case i = 'odd' : return 'blue';

    return 'red';



    Hope it works - Happy coding !!






    share|improve this answer












    Try something like this



    getColor(i)
    if (i % 2 === 0 && i != 0)i = 'odd';
    if (this.courses && (this.courses.length - 1 === i)) i = 'last'
    switch (i)
    case i = 0 : return 'orange';
    case i = 'odd' : return 'blue';

    return 'red';



    Hope it works - Happy coding !!







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 13 '18 at 3:31









    RahulRahul

    1,0231315




    1,0231315











    • You can use it as else if in case to avoid another condition :)
      – Rahul
      Nov 13 '18 at 3:45
















    • You can use it as else if in case to avoid another condition :)
      – Rahul
      Nov 13 '18 at 3:45















    You can use it as else if in case to avoid another condition :)
    – Rahul
    Nov 13 '18 at 3:45




    You can use it as else if in case to avoid another condition :)
    – Rahul
    Nov 13 '18 at 3:45













    1














    If you only want cange the background-color you can use [style.background-color] and you can use ternary operator in the .html



    <ul *ngFor="let course of courses; let index=i;
    let odd=odd;
    let last=last;
    let first=first">
    <li [style.backgound-color]="first?'orange':last?'purple':odd?'blue':'red'">
    ...
    </li>
    </ul>





    share|improve this answer




















    • That's one good way to do it, I wanted to do it this way first but I didn't know you could use more than 1 condition in a short IF on a bind like that. By the way, is there a difference in performance with either ways? I understand this is just a style tho but what if we have ALOT Of values
      – Jean-Philippe Dufour
      Nov 13 '18 at 14:13











    • If you has a lot of values, you can use [className]="first?'class1':... where 'class1', 'class2' are style defining in your component.css. In perfomance a ternary opertor is always faster. I prefer not make a call to a function, but I'm not sure about performance
      – Eliseo
      Nov 16 '18 at 13:14










    • thanks for your input. I will defenitly look this up.
      – Jean-Philippe Dufour
      Nov 17 '18 at 6:06















    1














    If you only want cange the background-color you can use [style.background-color] and you can use ternary operator in the .html



    <ul *ngFor="let course of courses; let index=i;
    let odd=odd;
    let last=last;
    let first=first">
    <li [style.backgound-color]="first?'orange':last?'purple':odd?'blue':'red'">
    ...
    </li>
    </ul>





    share|improve this answer




















    • That's one good way to do it, I wanted to do it this way first but I didn't know you could use more than 1 condition in a short IF on a bind like that. By the way, is there a difference in performance with either ways? I understand this is just a style tho but what if we have ALOT Of values
      – Jean-Philippe Dufour
      Nov 13 '18 at 14:13











    • If you has a lot of values, you can use [className]="first?'class1':... where 'class1', 'class2' are style defining in your component.css. In perfomance a ternary opertor is always faster. I prefer not make a call to a function, but I'm not sure about performance
      – Eliseo
      Nov 16 '18 at 13:14










    • thanks for your input. I will defenitly look this up.
      – Jean-Philippe Dufour
      Nov 17 '18 at 6:06













    1












    1








    1






    If you only want cange the background-color you can use [style.background-color] and you can use ternary operator in the .html



    <ul *ngFor="let course of courses; let index=i;
    let odd=odd;
    let last=last;
    let first=first">
    <li [style.backgound-color]="first?'orange':last?'purple':odd?'blue':'red'">
    ...
    </li>
    </ul>





    share|improve this answer












    If you only want cange the background-color you can use [style.background-color] and you can use ternary operator in the .html



    <ul *ngFor="let course of courses; let index=i;
    let odd=odd;
    let last=last;
    let first=first">
    <li [style.backgound-color]="first?'orange':last?'purple':odd?'blue':'red'">
    ...
    </li>
    </ul>






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 13 '18 at 8:13









    EliseoEliseo

    5,5491312




    5,5491312











    • That's one good way to do it, I wanted to do it this way first but I didn't know you could use more than 1 condition in a short IF on a bind like that. By the way, is there a difference in performance with either ways? I understand this is just a style tho but what if we have ALOT Of values
      – Jean-Philippe Dufour
      Nov 13 '18 at 14:13











    • If you has a lot of values, you can use [className]="first?'class1':... where 'class1', 'class2' are style defining in your component.css. In perfomance a ternary opertor is always faster. I prefer not make a call to a function, but I'm not sure about performance
      – Eliseo
      Nov 16 '18 at 13:14










    • thanks for your input. I will defenitly look this up.
      – Jean-Philippe Dufour
      Nov 17 '18 at 6:06
















    • That's one good way to do it, I wanted to do it this way first but I didn't know you could use more than 1 condition in a short IF on a bind like that. By the way, is there a difference in performance with either ways? I understand this is just a style tho but what if we have ALOT Of values
      – Jean-Philippe Dufour
      Nov 13 '18 at 14:13











    • If you has a lot of values, you can use [className]="first?'class1':... where 'class1', 'class2' are style defining in your component.css. In perfomance a ternary opertor is always faster. I prefer not make a call to a function, but I'm not sure about performance
      – Eliseo
      Nov 16 '18 at 13:14










    • thanks for your input. I will defenitly look this up.
      – Jean-Philippe Dufour
      Nov 17 '18 at 6:06















    That's one good way to do it, I wanted to do it this way first but I didn't know you could use more than 1 condition in a short IF on a bind like that. By the way, is there a difference in performance with either ways? I understand this is just a style tho but what if we have ALOT Of values
    – Jean-Philippe Dufour
    Nov 13 '18 at 14:13





    That's one good way to do it, I wanted to do it this way first but I didn't know you could use more than 1 condition in a short IF on a bind like that. By the way, is there a difference in performance with either ways? I understand this is just a style tho but what if we have ALOT Of values
    – Jean-Philippe Dufour
    Nov 13 '18 at 14:13













    If you has a lot of values, you can use [className]="first?'class1':... where 'class1', 'class2' are style defining in your component.css. In perfomance a ternary opertor is always faster. I prefer not make a call to a function, but I'm not sure about performance
    – Eliseo
    Nov 16 '18 at 13:14




    If you has a lot of values, you can use [className]="first?'class1':... where 'class1', 'class2' are style defining in your component.css. In perfomance a ternary opertor is always faster. I prefer not make a call to a function, but I'm not sure about performance
    – Eliseo
    Nov 16 '18 at 13:14












    thanks for your input. I will defenitly look this up.
    – Jean-Philippe Dufour
    Nov 17 '18 at 6:06




    thanks for your input. I will defenitly look this up.
    – Jean-Philippe Dufour
    Nov 17 '18 at 6:06











    0














    Thanks Rahul. The part I was missing is evaluating if there is something in courses. However, I had to had a few more lines to Odd and Last as follow :







    getColor(i)
    if (this.courses && i != 0 && (this.courses.length - 1 === i)) i = 'last'
    if (i % 2 === 0 && i != 0 && i != 'last')i = 'odd';
    switch (i)
    case i = 0 : return 'orange';
    case i = 'odd' : return 'blue';
    case i = 'last' : return 'purple';

    return 'red';



    Quick question. It seems like a whole lot of IF and && and checking specific things. Is that the way to do it properly?






    share|improve this answer

























      0














      Thanks Rahul. The part I was missing is evaluating if there is something in courses. However, I had to had a few more lines to Odd and Last as follow :







      getColor(i)
      if (this.courses && i != 0 && (this.courses.length - 1 === i)) i = 'last'
      if (i % 2 === 0 && i != 0 && i != 'last')i = 'odd';
      switch (i)
      case i = 0 : return 'orange';
      case i = 'odd' : return 'blue';
      case i = 'last' : return 'purple';

      return 'red';



      Quick question. It seems like a whole lot of IF and && and checking specific things. Is that the way to do it properly?






      share|improve this answer























        0












        0








        0






        Thanks Rahul. The part I was missing is evaluating if there is something in courses. However, I had to had a few more lines to Odd and Last as follow :







        getColor(i)
        if (this.courses && i != 0 && (this.courses.length - 1 === i)) i = 'last'
        if (i % 2 === 0 && i != 0 && i != 'last')i = 'odd';
        switch (i)
        case i = 0 : return 'orange';
        case i = 'odd' : return 'blue';
        case i = 'last' : return 'purple';

        return 'red';



        Quick question. It seems like a whole lot of IF and && and checking specific things. Is that the way to do it properly?






        share|improve this answer












        Thanks Rahul. The part I was missing is evaluating if there is something in courses. However, I had to had a few more lines to Odd and Last as follow :







        getColor(i)
        if (this.courses && i != 0 && (this.courses.length - 1 === i)) i = 'last'
        if (i % 2 === 0 && i != 0 && i != 'last')i = 'odd';
        switch (i)
        case i = 0 : return 'orange';
        case i = 'odd' : return 'blue';
        case i = 'last' : return 'purple';

        return 'red';



        Quick question. It seems like a whole lot of IF and && and checking specific things. Is that the way to do it properly?







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 '18 at 3:43









        Jean-Philippe DufourJean-Philippe Dufour

        297




        297





















            0














            You could use if else ladder instead of mixing up if else and switch and assignments like given below



            getColor(i)

            if(this.courses)

            if(i==0)
            return "orange";
            else if(i==this.courses.length-1)
            return "purple";
            else if (i%2==0)
            return "red";
            else
            return "blue";







            share|improve this answer
















            • 1




              Would def make a cleaner and easier to understand code. Thanks for your input.
              – Jean-Philippe Dufour
              Nov 13 '18 at 5:38










            • When you have simpler alternatives, don't mix up and always welcome :)
              – Mithil Mohan
              Nov 13 '18 at 5:39















            0














            You could use if else ladder instead of mixing up if else and switch and assignments like given below



            getColor(i)

            if(this.courses)

            if(i==0)
            return "orange";
            else if(i==this.courses.length-1)
            return "purple";
            else if (i%2==0)
            return "red";
            else
            return "blue";







            share|improve this answer
















            • 1




              Would def make a cleaner and easier to understand code. Thanks for your input.
              – Jean-Philippe Dufour
              Nov 13 '18 at 5:38










            • When you have simpler alternatives, don't mix up and always welcome :)
              – Mithil Mohan
              Nov 13 '18 at 5:39













            0












            0








            0






            You could use if else ladder instead of mixing up if else and switch and assignments like given below



            getColor(i)

            if(this.courses)

            if(i==0)
            return "orange";
            else if(i==this.courses.length-1)
            return "purple";
            else if (i%2==0)
            return "red";
            else
            return "blue";







            share|improve this answer












            You could use if else ladder instead of mixing up if else and switch and assignments like given below



            getColor(i)

            if(this.courses)

            if(i==0)
            return "orange";
            else if(i==this.courses.length-1)
            return "purple";
            else if (i%2==0)
            return "red";
            else
            return "blue";








            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 13 '18 at 5:30









            Mithil MohanMithil Mohan

            1389




            1389







            • 1




              Would def make a cleaner and easier to understand code. Thanks for your input.
              – Jean-Philippe Dufour
              Nov 13 '18 at 5:38










            • When you have simpler alternatives, don't mix up and always welcome :)
              – Mithil Mohan
              Nov 13 '18 at 5:39












            • 1




              Would def make a cleaner and easier to understand code. Thanks for your input.
              – Jean-Philippe Dufour
              Nov 13 '18 at 5:38










            • When you have simpler alternatives, don't mix up and always welcome :)
              – Mithil Mohan
              Nov 13 '18 at 5:39







            1




            1




            Would def make a cleaner and easier to understand code. Thanks for your input.
            – Jean-Philippe Dufour
            Nov 13 '18 at 5:38




            Would def make a cleaner and easier to understand code. Thanks for your input.
            – Jean-Philippe Dufour
            Nov 13 '18 at 5:38












            When you have simpler alternatives, don't mix up and always welcome :)
            – Mithil Mohan
            Nov 13 '18 at 5:39




            When you have simpler alternatives, don't mix up and always welcome :)
            – Mithil Mohan
            Nov 13 '18 at 5:39

















            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%2f53273162%2fangular-6-ngfor-display-different-styles-for-first-odd-even-and-last%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







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3

            Museum of Modern and Contemporary Art of Trento and Rovereto