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
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"> </td> </tr>– Sujay
Aug 1 '18 at 8:34
<tr class="blank_row"> <td bgcolor="#FFFFFF" colspan="3"> </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.
Please show you
Studentclass/interface– trichetriche
Aug 1 '18 at 8:03