How to push a new Item to an empty array?

How to push a new Item to an empty array?



I have an array of studets that I use by *ngFor,
The student list send bi the parent component,
when the studentList is empty, I want to see one row empty on the component,
This is the array:


*ngFor


@Input() studentList: Student = ; //child property



And In The parent cmp:


get studentList(): Student //parent property
const arr: Array<Student> = new Array<Student>();
if (getStudents())
arr=getStudents();

else
arr.push(new Student());
return (arr);




getStudents() is a Mock method that return null,
Student is a simple class with idNumber and studentName props
but if the arr is empty, the *ngFor does not show any row.


getStudents()


Student


idNumber


studentName


*ngFor





Please show you Student class/interface
– trichetriche
Aug 1 '18 at 8:03


Student





And your getStudents() method too
– trichetriche
Aug 1 '18 at 8:04


getStudents()





And how can you have both a variable and a getter with the same name ?
– trichetriche
Aug 1 '18 at 8:04





use an *ngIf="studentList.length" and handle go to for loop or not.
– Fateme Fazli
Aug 1 '18 at 8:06


*ngIf="studentList.length"





I updated the question by the comments.
– user1012506
Aug 1 '18 at 8:20




4 Answers
4



What you can do in this case is use



*ngIf=studentList.length === 0 use this condition for showing empty row


*ngIf=studentList.length === 0



*ngIf=studentList.length > 0 use this condition when data is available.


*ngIf=studentList.length > 0





why down vote ?
– Sujay
Aug 1 '18 at 8:10





when *ngIf=studentList.length === 0, how can show empty row?? this is my question...
– user1012506
Aug 1 '18 at 8:28





if you are using a table you can use <tr class="blank_row"> <td bgcolor="#FFFFFF" colspan="3">&nbsp;</td> </tr>
– Sujay
Aug 1 '18 at 8:34



<tr class="blank_row"> <td bgcolor="#FFFFFF" colspan="3">&nbsp;</td> </tr>





empty row that can updateable the new student, not empty line
– user1012506
Aug 1 '18 at 8:36



You can try this one:


get studentList(): Student
const arr: Array<Student> = ;
if (arr.length)
arr = getStudents();
else
arr = [new Student()];

return arr; // return it here.



Need to declare the Student as ;


get studentList(): Student //parent property
const arr: Student =
if (getStudents())
arr=getStudents();

else
arr.push(new Student());
return (arr);




you can do something like this in your html file


<table>
<thead>
------
</thead>
<tbody>
<tr *ngFor="student of students">
</tr>
<tr *ngIf="students.length==0">
</tr>
</tbody>
</table>



Thanks for contributing an answer to Stack Overflow!



But avoid



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



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



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



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ

How do I collapse sections of code in Visual Studio Code for Windows?